Пример #1
0
        /// <summary>
        /// Sorts an IQueryable by one or more fields.
        /// </summary>
        /// <typeparam name="T">The collection type.</typeparam>
        /// <param name="source">The collection to be sorted.</param>
        /// <param name="request">An ISortRequest with one or more Sort objects specifying a field and direction by which to order the collection.</param>
        /// <returns>the IQueryable, sorted as specified by the ISortRequest.</returns>
        public static IOrderedQueryable <T> Sort <T>(this IQueryable <T> source, ISortRequest request)
        {
            if (request != null && request.Sorts != null)
            {
                return(source.Sort(request.Sorts));
            }

            return(source as IOrderedQueryable <T>);
        }
Пример #2
0
        public static IOrderedQueryable <T> Sort <T>(this IQueryable <T> source, ISortRequest request)
        {
            var dest = source as IOrderedQueryable <T>;

            if (request.Sorts == null || !request.Sorts.Any())
            {
                return(dest);
            }

            var sortingList = request.Sorts.ToList();
            var itemType    = typeof(T);
            var parameter   = Expression.Parameter(itemType, "item");

            for (var i = 0; i < sortingList.Count; i++)
            {
                var property                = typeof(T).GetProperty(sortingList[i].Field);
                var propertyAccess          = Expression.MakeMemberAccess(parameter, property);
                var sortExpression          = Expression.Lambda(propertyAccess, parameter);
                MethodCallExpression result = null;

                if (i == 0)
                {
                    switch (sortingList[i].Direction)
                    {
                    case SortDirection.Ascending:
                        result = Expression.Call(typeof(Queryable), "OrderBy", new Type[] { itemType, property.PropertyType }, dest.Expression, Expression.Quote(sortExpression));
                        break;

                    case SortDirection.Descending:
                        result = Expression.Call(typeof(Queryable), "OrderByDescending", new Type[] { itemType, property.PropertyType }, dest.Expression, Expression.Quote(sortExpression));
                        break;
                    }
                }
                else
                {
                    switch (sortingList[i].Direction)
                    {
                    case SortDirection.Ascending:
                        result = Expression.Call(typeof(Queryable), "ThenBy", new Type[] { itemType, property.PropertyType }, dest.Expression, Expression.Quote(sortExpression));
                        break;

                    case SortDirection.Descending:
                        result = Expression.Call(typeof(Queryable), "ThenByDescending", new Type[] { itemType, property.PropertyType }, dest.Expression, Expression.Quote(sortExpression));
                        break;
                    }
                }

                dest = dest.Provider.CreateQuery <T>(result) as IOrderedQueryable <T>;
            }

            return(dest);
        }
        public static ITypeSearch <T> SortBy <T>(this ITypeSearch <T> query, ISortRequest request) where T : IContent
        {
            if (request?.Sorts?.Items == null)
            {
                return(query);
            }

            //foreach (var sort in (from item in request.Sorts.Items
            //                      where !OrderBy(ref query, item)
            //                      select item))
            //{
            //    throw new NotImplementedException(string.Format("Type {0} is not implemented for sort by!", sort.GetType()));
            //}

            return(query);
        }
Пример #4
0
 public SqlOrderByBuilder(string defaultOrderBy, Dictionary <string, string> columns, ISortRequest request)
 {
     _defaultOrderBy = defaultOrderBy;
     _columns        = columns;
     _request        = request;
 }