Example #1
0
        public static IQueryable <T> OrderBy <T>(this IQueryable <T> source, params ISorting[] sortings)
        {
            IEntitySorter <T> sorter = null;

            if (sortings != null)
            {
                foreach (var sorting in sortings)
                {
                    if (sorter == null)
                    {
                        sorter = sorting.Ascending
                            ? EntitySorter <T> .OrderBy(sorting.ColumnName)
                            : EntitySorter <T> .OrderByDescending(sorting.ColumnName);
                    }
                    else
                    {
                        sorter = sorting.Ascending
                            ? sorter.ThenBy(sorting.ColumnName)
                            : sorter.ThenByDescending(sorting.ColumnName);
                    }
                }
                return(sorter != null?sorter.Sort(source) : source);
            }
            return(source);
        }
        //public static IQueryable<T> ApplyPaging<T>(this IQueryable<T> data, int itemsDisplayed, int pageSize)
        //{
        //    if (pageSize > 0 && itemsDisplayed > 0)
        //        data = data.Skip(itemsDisplayed);

        //    data = data.Take(pageSize);

        //    return data;
        //}

        //public static IQueryable<T> ApplySorting<T>(this IQueryable<T> data, string sortDirection, Expression<Func<T, string>> orderingFunction)
        //{
        //    return sortDirection == "asc" ? data.OrderBy(orderingFunction) : data.OrderByDescending(orderingFunction);
        //}


        /// <summary>
        /// 透過 <see cref="EntitySorter"/>,作到動態屬性名稱的排序
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="source"></param>
        /// <param name="criteria"></param>
        /// <param name="defaultKeySelector"></param>
        /// <param name="isDescending"></param>
        /// <returns></returns>
        public static IOrderedQueryable <TEntity> SortByCriteria <TEntity, TKey>(this IQueryable <TEntity> source,
                                                                                 QueryCriteriaModel criteria, Expression <Func <TEntity, TKey> > defaultKeySelector, bool isDescending = true)
        {
            IEntitySorter <TEntity> sorter;

            if (string.IsNullOrEmpty(criteria.Sort))
            {
                sorter = (isDescending)
                            ? EntitySorter <TEntity> .OrderByDescending(defaultKeySelector)
                            : EntitySorter <TEntity> .OrderBy(defaultKeySelector);
            }
            else
            {
                // 動態利用 EntitySorter 來排序~
                sorter = (criteria.IsDescending)
                     ? EntitySorter <TEntity> .OrderByDescending(criteria.Sort)
                     : EntitySorter <TEntity> .OrderBy(criteria.Sort);
            }

            var sortedResult = sorter.Sort(source);

            //Console.WriteLine(sortedResult.ToString());

            return(sortedResult);
        }
Example #3
0
        public static IQueryable <T> OrderBy <T>(this IQueryable <T> entities, SortDirection sortDirection, string sortField)
        {
            switch (sortDirection)
            {
            case SortDirection.Asc:
                return(EntitySorter <T> .OrderBy(sortField).Sort(entities));

            case SortDirection.Desc:
                return(EntitySorter <T> .OrderByDescending(sortField).Sort(entities));

            default:
                return(entities);
            }
        }
 public static IEntitySorter <T> OrderBy <T, TKey>(
     this IEntitySorter <T> sorter,
     Expression <Func <T, TKey> > keySelector)
 {
     return(EntitySorter <T> .OrderBy(keySelector));
 }