public static IQueryable <T> ApplySortingPaging <T>(this IOrderedQueryable <T> query, SortingPagingDto sortingPaging)
        {
            var firstPass = true;

            foreach (var sortOrder in sortingPaging.SortOrders)
            {
                if (firstPass)
                {
                    firstPass = false;
                    query     = sortOrder.ColumnOrder == SortOrderDto.SortOrder.Ascending
                                ? query.OrderBy(sortOrder.ColumnName) :
                                query.OrderByDescending(sortOrder.ColumnName);
                }
                else
                {
                    query = sortOrder.ColumnOrder == SortOrderDto.SortOrder.Ascending
                                ? query.ThenBy(sortOrder.ColumnName) :
                            query.ThenByDescending(sortOrder.ColumnName);
                }
            }

            //var result = query.Skip((sortingPaging.PageNumber - 1) *
            //  sortingPaging.NumberRecords).Take(sortingPaging.NumberRecords);

            return(query.AsQueryable());
        }
        public static IQueryable <TEntity> SortQuery <TEntity>(this IQueryable <TEntity> query, IList <SortOrderModel> sortOrders)
        {
            var paramExpression = Expression.Parameter(typeof(TEntity), "o");
            IOrderedQueryable <TEntity> sortedQuery = null;

            for (int i = 0; i < sortOrders.Count; i++)
            {
                var order = sortOrders[i];
                var propertyExpression = Expression.PropertyOrField(paramExpression, order.Field);
                var convertExpression  = Expression.Convert(propertyExpression, typeof(object));

                var lambdaExpression = Expression.Lambda <Func <TEntity, object> >(convertExpression, paramExpression);

                if (order.Direction == SortDirectionEnum.ASC)
                {
                    sortedQuery = i == 0 ? query.OrderBy(lambdaExpression) : sortedQuery.ThenBy(lambdaExpression);
                }
                else
                {
                    sortedQuery = i == 0 ? query.OrderByDescending(lambdaExpression) : sortedQuery.ThenByDescending(lambdaExpression);
                }
            }

            return(sortedQuery.AsQueryable());
        }
Beispiel #3
0
        public static IQueryable <T> ApplySort <T>(this IQueryable <T> queryable, Dictionary <Expression <Func <T, object> >, ListSortDirection> sort)
        {
            if (sort != null)
            {
                IOrderedQueryable <T> orderedQueryable = null;

                foreach (var sortProperty in sort)
                {
                    if (sortProperty.Value == ListSortDirection.Ascending)
                    {
                        orderedQueryable = orderedQueryable is null
                            ? queryable.OrderBy(sortProperty.Key)
                            : orderedQueryable.ThenBy(sortProperty.Key);
                    }
                    else
                    {
                        orderedQueryable = orderedQueryable is null
                            ? queryable.OrderByDescending(sortProperty.Key)
                            : orderedQueryable.ThenByDescending(sortProperty.Key);
                    }
                }

                queryable = orderedQueryable is null
                    ? queryable
                    : orderedQueryable.AsQueryable();
            }

            return(queryable.AsQueryable());
        }
Beispiel #4
0
        protected void ApplyOrderByExpressions(ref IQueryable <TEntity> query, params OrderByExpression <TEntity>[] orderByExpressions)
        {
            //append order bys
            if (orderByExpressions == null)
            {
                return;
            }

            IOrderedQueryable <TEntity> queryOrdered = null;
            var isFirstExpr = true;

            foreach (var obe in orderByExpressions)
            {
                if (isFirstExpr)
                {
                    queryOrdered = obe.IsDescending ?
                                   query.OrderByDescending(obe.Expression) :
                                   query.OrderBy(obe.Expression);
                    isFirstExpr = false;
                }
                else
                {
                    queryOrdered = obe.IsDescending ?
                                   queryOrdered.ThenByDescending(obe.Expression) :
                                   queryOrdered.ThenBy(obe.Expression);
                }
            }
            //now convert it back to iqueryable
            query = queryOrdered.AsQueryable <TEntity>();
        }
Beispiel #5
0
        /// <summary>
        /// This method retrieves all Branches.
        /// Change this method to alter how records are retrieved.
        /// </summary>
        public IQueryable <Branch> GetAllBranches()
        {
            IOrderedQueryable <Branch> query = from branch in DbContext.Branches
                                               orderby branch.Name
                                               select branch;

            return(query.AsQueryable());
            // return DbContext.Branches.;
        }
Beispiel #6
0
    void IListServer.Apply(DevExpress.Data.Filtering.CriteriaOperator filterCriteria, ICollection <ServerModeOrderDescriptor[]> sortInfo, int groupCount, ICollection <ServerModeSummaryDescriptor> groupSummaryInfo, ICollection <ServerModeSummaryDescriptor> totalSummaryInfo)
    {
        isCountDirty  = true;
        isResultDirty = true;
        isKeysDirty   = true;

        count         = -1;
        startKeyIndex = -1;
        startRowIndex = -1;

        this.query = this.origQuery;

        if (sortInfo != null)
        {
            Boolean first = true;
            IOrderedQueryable <Contact> oq = null;

            foreach (ServerModeOrderDescriptor[] info in sortInfo)
            {
                if (info.Length > 1)
                {
                    throw new NotImplementedException("Multi-grouping is not supported by ASPxGridView");
                }
                String propertyName = (info[0].SortExpression as OperandProperty).PropertyName;

                if (first)
                {
                    if (info[0].IsDesc)
                    {
                        oq = LinqHelper.OrderByDescending <Contact>(this.query, propertyName);
                    }
                    else
                    {
                        oq = LinqHelper.OrderBy <Contact>(this.query, propertyName);
                    }

                    first = false;
                }
                else
                {
                    if (info[0].IsDesc)
                    {
                        oq = LinqHelper.ThenByDescending <Contact>(oq, propertyName);
                    }
                    else
                    {
                        oq = LinqHelper.ThenBy <Contact>(oq, propertyName);
                    }
                }
            }

            this.query = oq.AsQueryable <Contact>();
        }
    }
Beispiel #7
0
        private static IOrderedQueryable <T> OrderBy <T>(this IQueryable <T> source, PageModel pageModel)
        {
            IOrderedQueryable <T> sorted = null;

            for (var index = 0; index < pageModel.SortFields.Count; index++)
            {
                sorted = source.OrderByOrThenBy <T>(pageModel.SortFields[index], index);
                source = sorted.AsQueryable();
            }

            return(sorted);
        }
Beispiel #8
0
 public IQueryable <T> GetAll <T>(string sortColumn, string sortOrder)
     where T : class, IEntityWithKey
 {
     if (string.IsNullOrEmpty(sortColumn))
     {
         return(GetAll <T>().AsQueryable());
     }
     else
     {
         IOrderedQueryable <T> objectquery = _container.CreateObjectSet <T>().OrderBy("it." + sortColumn + " " + sortOrder);
         return(objectquery.AsQueryable());
     }
 }
Beispiel #9
0
        public static EntityList <TEntity> GetPagedEntitys <TEntity, Sort1, Sort2>(DbContext db, int pageIndex, int pageSize, Expression <Func <TEntity, bool> > whereLambds, bool isAsc1, Expression <Func <TEntity, Sort1> > orderByLambd1, bool isAsc2, Expression <Func <TEntity, Sort2> > orderByLambd2) where TEntity : class, new()
        {
            var temp      = db.Set <TEntity>().Where <TEntity>(whereLambds);
            var rows      = temp.Count();
            var totalPage = rows % pageSize == 0 ? rows / pageSize : rows / pageSize + 1;

            IOrderedQueryable <TEntity> temp1 = isAsc1 ? temp.OrderBy <TEntity, Sort1>(orderByLambd1) : temp.OrderByDescending <TEntity, Sort1>(orderByLambd1);

            temp1 = isAsc2 ? temp1.ThenBy <TEntity, Sort2>(orderByLambd2) : temp1.ThenByDescending <TEntity, Sort2>(orderByLambd2);

            var temp2 = temp1.AsQueryable <TEntity>().Skip <TEntity>(pageSize * (pageIndex - 1)).Take <TEntity>(pageSize);

            var list     = temp2.ToList <TEntity>();
            var dataList = Activator.CreateInstance(typeof(EntityList <TEntity>)) as EntityList <TEntity>;

            dataList.List       = list;
            dataList.TotalRows  = rows;
            dataList.TotalPages = totalPage;
            return(dataList);
        }
Beispiel #10
0
        private static IQueryable <TEntity> GetQuery(IQueryable <TEntity> query, IList <SortOrderModel> sortOrders, Expression <Func <TEntity, object> >[] includes)
        {
            if (includes != null)
            {
                foreach (var include in includes)
                {
                    query = query.Include(include);
                }
            }

            if (sortOrders != null && sortOrders.Any())
            {
                var paramExpression = Expression.Parameter(typeof(TEntity), "o");
                IOrderedQueryable <TEntity> sortedQuery = null;

                for (int i = 0; i < sortOrders.Count; i++)
                {
                    var order = sortOrders[i];
                    var propertyExpression = Expression.PropertyOrField(paramExpression, order.Field);
                    var convertExpression  = Expression.Convert(propertyExpression, typeof(object));

                    var lambdaExpression = Expression.Lambda <Func <TEntity, object> >(convertExpression, paramExpression);

                    if (order.Direction == SortDirectionEnum.ASC)
                    {
                        sortedQuery = i == 0 ? query.OrderBy(lambdaExpression) : sortedQuery.ThenBy(lambdaExpression);
                    }
                    else
                    {
                        sortedQuery = i == 0 ? query.OrderByDescending(lambdaExpression) : sortedQuery.ThenByDescending(lambdaExpression);
                    }
                }

                query = sortedQuery.AsQueryable();
            }

            return(query);
        }
 public static async Task <PagedResult <T> > PaginateAsync <T>(this IOrderedQueryable <T> collection,
                                                               int page = 1, int resultsPerPage = 10)
 {
     return(await collection.AsQueryable().PaginateAsync(page, resultsPerPage));
 }
Beispiel #12
0
 public static PagedResult <T> Paginate <T>(this IOrderedQueryable <T> source, Paging paging)
 {
     return(source.AsQueryable().Paginate(paging));
 }
Beispiel #13
0
 public static async Task <PagedResult <T> > PaginateAsync <T>(this IOrderedQueryable <T> source, Paging paging)
 {
     return(await source.AsQueryable().PaginateAsync(paging));
 }
Beispiel #14
0
        public static IQueryable <T> Sort <T>(IQueryable <T> query, IEnumerable <SortableItem> sortables) where T : AbstractEntity
        {
            IOrderedQueryable <T> _query = null;

            foreach (SortableItem sortable in sortables)
            {
                if (sortable.Mode != 0)
                {
                    Console.WriteLine(sortable.Name);

                    var type     = typeof(T);
                    var property = type.GetProperty(sortable.Name);

                    var _parameter = Expression.Parameter(type);
                    var _property  = Expression.Property(_parameter, property);

                    Console.WriteLine(type + ":" + property.PropertyType.ToString());

                    switch (property.PropertyType.ToString())
                    {
                    case ("System.Int32"):
                    {
                        By(ref query, ref _query, sortable.Mode,
                           Expression.Lambda <Func <T, int> >(_property, _parameter));

                        break;
                    }

                    case ("System.Int64"):
                    {
                        By(ref query, ref _query, sortable.Mode,
                           Expression.Lambda <Func <T, long> >(_property, _parameter));

                        break;
                    }

                    case ("System.String"):
                    {
                        By(ref query, ref _query, sortable.Mode,
                           Expression.Lambda <Func <T, string> >(_property, _parameter));

                        break;
                    }

                    case ("System.DateTime"):
                    {
                        By(ref query, ref _query, sortable.Mode,
                           Expression.Lambda <Func <T, DateTime> >(_property, _parameter));

                        break;
                    }

                    default:
                        Console.WriteLine("未支持的排序类型.");
                        break;
                    }
                }
            }

            Console.WriteLine("完成排序组装");

            if (_query != null)
            {
                return(_query.AsQueryable());
            }

            return(query);
        }
 public static IOrderedQueryable <TSource> ThenBy <TSource, TProp>(this IOrderedQueryable <TSource> query, ISortParams sortParams, string prop, Expression <Func <TSource, TProp> > expression)
 {
     return(query.AsQueryable().OrderBy(sortParams, prop, expression));
 }