public PageDefinition(IPagerFilter pagerFilter, int itemCount) { ItemCount = itemCount; PageSize = Calculate.ValidPageSize(pagerFilter.PageSize); PageIndex = Calculate.ValidPageIndex(pagerFilter.PageIndex, pagerFilter.PageSize, itemCount); PageCount = Calculate.TotalPageCount(itemCount, PageSize); SkipIndex = Calculate.SkipIndex(pagerFilter.PageIndex, pagerFilter.PageSize); }
/// <summary> /// Converts IEnumerable to a Page /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source"></param> /// <param name="pagerFilter"><see cref="IPagerFilter"/></param> /// <returns></returns> public static Page <T> ToPage <T>(this IEnumerable <T> source, IPagerFilter pagerFilter) { Validator.Validate(pagerFilter); var pd = new PageDefinition(pagerFilter, source.Count()); return(pd.ItemCount < 1 ? Page <T> .FromEnumerable(source, pd) : Page <T> .FromEnumerable(source.Skip(pd.SkipIndex).Take(pd.PageSize), pd)); }
/// <summary> /// Converts IQueryable to a Page /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source"></param> /// <param name="pagerFilter"><see cref="IPagerFilter"/></param> /// <param name="itemCount">Total item count of IQueryable</param> /// <param name="prePaged">Is the IQueryable already prepaged. If so the IQueryable will not be paged anymore.</param> /// <returns></returns> public static Page <T> ToPage <T>(this IQueryable <T> source, IPagerFilter pagerFilter, int itemCount, bool prePaged = false) { Validator.Validate(pagerFilter); var pd = new PageDefinition(pagerFilter, itemCount); return(prePaged || pd.ItemCount < 1 ? Page <T> .FromEnumerable(source, pd) : Page <T> .FromEnumerable(source.Skip(pd.SkipIndex).Take(pd.PageSize), pd)); }
internal static void Validate(IPagerFilter pagerFilter) { if (pagerFilter.PageIndex < 1) { throw new ArgumentOutOfRangeException("IPagerFilter.PageIndex", "Can not be less then zero or zero."); } if (pagerFilter.PageSize < 1) { throw new ArgumentOutOfRangeException("IPagerFilter.PageSize", "Can not be less then zero or zero."); } }
/// <summary> /// Converts asynchronous IEnumerable to a Page /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source"></param> /// <param name="pagerFilter"><see cref="IPagerFilter"/></param> /// <param name="itemCount">Total item count of IEnumerable</param> /// <param name="prePaged">Is the IEnumerable already prepaged. If so the IEnumerable will not be paged anymore.</param> /// <returns></returns> public static async Task <Page <T> > ToPageAsync <T>(this IEnumerable <T> source, IPagerFilter pagerFilter, int itemCount, bool prePaged = false) { Validator.Validate(pagerFilter); var pd = new PageDefinition(pagerFilter, itemCount); return(prePaged || pd.ItemCount < 1 ? await Task.Run(() => Page <T> .FromEnumerable(source, pd)).ConfigureAwait(false) : await Task.Run(() => Page <T> .FromEnumerable(source.Skip(pd.SkipIndex).Take(pd.PageSize), pd)).ConfigureAwait(false)); }