Пример #1
0
        public static PaginatedResult <TDest> Paginate <TSource, TDest>(this IQueryable <TSource> query, Pagination pagination, Func <TSource, TDest> map, SortDefinition sort = null)
        {
            if (query == null)
            {
                return(null);
            }

            if (map == null)
            {
                throw new ArgumentNullException(nameof(map));
            }
            var result = new PaginatedResult <TDest>();
            var set    = query;

            set = !string.IsNullOrEmpty(sort?.SortBy) ? set.Sort(sort) : set.OrderBy("Id");

            if (pagination != null)
            {
                result.Pagination            = pagination;
                result.Pagination.TotalCount = set.Count();

                set = set.Skip(pagination.ItemsPerPage * (pagination.Page - 1))
                      .Take(pagination.ItemsPerPage);
            }

            result.Items = set.AsEnumerable().Select(map).ToList();

            return(result);
        }
Пример #2
0
 public static PaginatedResult <T> Paginate <T>(this IQueryable <T> query, Pagination pagination, SortDefinition sort = null)
 {
     return(Paginate(query, pagination, x => x, sort));
 }