public Paged( IEnumerable <T> source, long totalItemsCount, int pageIndex = PageQuery.DefaultIndexFrom, int pageSize = PageQuery.DefaultPageSize, PageIndexFrom indexFrom = null, bool alreadyPaged = false) : this(source, null, null, totalItemsCount, pageIndex, pageSize, indexFrom, alreadyPaged) { }
public Paged( IEnumerable <T> source, IDictionary <string, string> fields, IEnumerable <string> keys, long totalItemsCount, int pageIndex = PageQuery.DefaultIndexFrom, int pageSize = PageQuery.DefaultPageSize, PageIndexFrom indexFrom = null, bool alreadyPaged = false) { var realSource = source ?? throw new ArgumentNullException(nameof(source)); PageIndex = pageIndex; PageSize = pageSize; IndexFrom = indexFrom ?? PageIndexFrom.One; if (IndexFrom.Id > PageIndex && PageIndex != 0) { throw new ArgumentException( $"indexFrom ({IndexFrom.Id}) > pageIndex ({pageIndex}), must indexFrom <= pageIndex"); } TotalItemsCount = totalItemsCount; PagesCount = (int)Math.Ceiling(TotalItemsCount / (double)PageSize); if (PageIndex > PagesCount) { PageIndex = PagesCount; } if (alreadyPaged) { Items = realSource.ToList(); } else { Items = realSource.Skip((PageIndex - IndexFrom.Id) * PageSize) .Take(PageSize) .ToList(); } Fields = fields; Keys = keys; }
public static PageQuery Get( int pageIndex = DefaultIndexFrom, int pageSize = DefaultPageSize, string[] sorts = null, string keyword = null, PageIndexFrom indexFrom = null, string[] searchFields = null) { if (pageSize < 1 || pageSize > MaxPageSize) { throw new ArgumentOutOfRangeException( nameof(pageSize), $"Please validate your pageSize. It must be greater than 0 and not exceed {MaxPageSize}"); } return(new PageQuery { PageIndex = pageIndex, PageSize = pageSize, Sorts = sorts, Keyword = keyword, IndexFrom = indexFrom ?? PageIndexFrom.One, SearchFields = searchFields }); }