Example #1
0
        /// <summary>
        /// Sorts IQueryable source according to provided sort criteria  and sort functor
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source">Source IQueryable</param>
        /// <param name="sortCriteria">SortCriteria to be used for sorting</param>
        /// <param name="sortFunctor">Sort functor to be used for sorting</param>
        /// <returns><see cref="IOrderedQueryable<typeparamref name="T"/>"/></returns>
        public static IOrderedQueryable <T> DoSorting <T>(this IQueryable <T> source, IEnumerable <SortCriteria> sortCriteria, Func <string, Expression <Func <T, object> > > sortFunctor)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            // Convert to IOrderedQueryable, will leave current order
            var ordered = source.OrderBy(x => 0);

            if (!ordered.Any() || sortCriteria == null || !sortCriteria.Any())
            {
                return(ordered);
            }

            foreach (var sortRule in sortCriteria.OrderBy(x => x.Priority))
            {
                ordered = SortingHelper.ApplySortRule(ordered, sortRule, sortFunctor);
            }

            return(ordered);
        }