Exemple #1
0
        public void DoOrder(Linq.Expression <Func <T, object> > defaultSortProperty)
        {
            var sortPropertyPath = SearchPackage.SortColumn;

            if (sortPropertyPath == null)
            {
                if (defaultSortProperty.Body is Linq.MemberExpression)
                {
                    sortPropertyPath = ((Linq.MemberExpression)defaultSortProperty.Body).Member.Name;
                }
                else
                {
                    var op = ((Linq.UnaryExpression)defaultSortProperty.Body).Operand;
                    sortPropertyPath = ((Linq.MemberExpression)op).Member.Name;
                }
            }
            if (string.IsNullOrWhiteSpace(sortPropertyPath))
            {
                return;
            }

            var isAscending = SearchPackage.SortSord == null ||
                              SearchPackage.SortSord.ToLowerInvariant().Contains("asc");

            var paramExpr       = Linq.Expression.Parameter(_queryable.ElementType, "p");
            var expr            = BuildPropertyExpression(paramExpr, sortPropertyPath);
            var orderByExpr     = Linq.Expression.Lambda(expr, new[] { paramExpr });
            var orderByCallExpr = Linq.Expression.Call(typeof(Queryable), isAscending ? "OrderBy" : "OrderByDescending",
                                                       new[] { _queryable.ElementType, expr.Type }, _queryable.Expression, orderByExpr);

            _queryable = _queryable.Provider.CreateQuery <T>(orderByCallExpr);
        }
Exemple #2
0
        public SearchPackageDataBuilder <T> SetFilterRule(Linq.Expression <Func <T, object> > filterProperty,
                                                          Func <string, Linq.Expression <Func <T, bool> > > filterAction)
        {
            var filterPropertyName = filterProperty.Body.ToString();

            filterPropertyName = filterPropertyName.Substring(filterPropertyName.IndexOf('.') + 1).TrimEnd(new [] { ')' });
            _filterActions.Add(filterPropertyName.ToLower(), filterAction);

            return(this);
        }
Exemple #3
0
        public List <T> GetExportResult(Linq.Expression <Func <T, object> > defaultSortProperty)
        {
            var recordsTotalCount = DoFilterData();

            DoOrder(defaultSortProperty);

            var gridResult = new List <T>();

            foreach (var item in _queryable)
            {
                gridResult.Add(item);
            }
            return(gridResult);
        }
Exemple #4
0
        public SearchPackageResult GetPageData(Linq.Expression <Func <T, object> > defaultSortProperty)
        {
            var recordsRequested = SearchPackage.RowCount;
            var pageIndex        = Math.Max(SearchPackage.PageIndex - 1, 0);

            var recordsTotalCount = DoFilterData();

            if (SearchPackage.TopRecords != 0)
            {
                recordsTotalCount = Math.Min(SearchPackage.TopRecords, recordsTotalCount);
            }

            var totalPages = 0;

            if (recordsRequested > 0)
            {
                totalPages = (int)Math.Ceiling(((float)recordsTotalCount) / recordsRequested);
            }

            DoOrder(defaultSortProperty);


            if (SearchPackage.TopRecords == 0)
            {
                _queryable = _queryable
                             .Skip(recordsRequested * pageIndex)
                             .Take(recordsRequested);
            }
            else
            {
                if (pageIndex == totalPages - 1)
                {
                    var numRecords = recordsTotalCount - recordsRequested * pageIndex;
                    _queryable = _queryable
                                 .Skip(recordsRequested * pageIndex)
                                 .Take(numRecords);
                }
                else
                {
                    _queryable = _queryable
                                 .Skip(recordsRequested * pageIndex)
                                 .Take(recordsRequested);
                }
            }

            var gridRowData = new List <SearchPackageResultRow>();

// ReSharper disable once LoopCanBeConvertedToQuery
            foreach (var item in _queryable)
            {
                gridRowData.Add(new SearchPackageResultRow()
                {
                    id   = _idMap(item),
                    cell = _cellMap(item)
                });
            }

            return(new SearchPackageResult()
            {
                page = pageIndex + 1,
                total = totalPages,
                records = recordsTotalCount,
                rows = gridRowData
            });
        }
Exemple #5
0
 public SearchPackageDataBuilder <T> Map(Linq.Expression <Func <T, object> > idExpression, Linq.Expression <Func <T, object> > cellExpression)
 {
     _idMap   = idExpression.Compile();
     _cellMap = cellExpression.Compile();
     return(this);
 }