| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Diagnostics.CodeAnalysis; |
| | | 3 | | using System.Linq; |
| | | 4 | | |
| | | 5 | | namespace Plainquire.Page; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Extension methods for <see cref="IQueryable{TEntity}"/> |
| | | 9 | | /// </summary> |
| | | 10 | | [SuppressMessage("ReSharper", "MemberCanBePrivate.Global", Justification = "Provided as library, can be used from outsid |
| | | 11 | | public static class QueryableExtensions |
| | | 12 | | { |
| | | 13 | | /// <inheritdoc cref="Page{TEntity}(IQueryable{TEntity}, uint?, uint?, IPageInterceptor?)"/> |
| | | 14 | | public static IEnumerable<TEntity> Page<TEntity>(this IEnumerable<TEntity> source, uint? pageNumber, uint? pageSize, |
| | 1 | 15 | | => source.AsQueryable().Page(new EntityPage(pageNumber, pageSize), interceptor); |
| | | 16 | | |
| | | 17 | | /// <inheritdoc cref="Page{TEntity}(IQueryable{TEntity}, EntityPage, IPageInterceptor?)"/>" |
| | | 18 | | public static IEnumerable<TEntity> Page<TEntity>(this IEnumerable<TEntity> source, EntityPage page, IPageInterceptor |
| | 127 | 19 | | => source.AsQueryable().Page(page, interceptor); |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Pages the elements of a sequence according to the given page number and size. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <typeparam name="TEntity"></typeparam> |
| | | 25 | | /// <param name="source">The elements to page.</param> |
| | | 26 | | /// <param name="pageNumber">The page number to retrieve.</param> |
| | | 27 | | /// <param name="pageSize">The page size to use.</param> |
| | | 28 | | /// <param name="interceptor">An interceptor to manipulate the generated page.</param> |
| | | 29 | | /// <returns></returns> |
| | | 30 | | public static IQueryable<TEntity> Page<TEntity>(this IQueryable<TEntity> source, uint? pageNumber, uint? pageSize, I |
| | 1 | 31 | | => source.Page(new EntityPage(pageNumber, pageSize), interceptor); |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Pages the elements of a sequence according to the given <paramref name="page"/>. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <typeparam name="TEntity"></typeparam> |
| | | 37 | | /// <param name="source">The elements to page.</param> |
| | | 38 | | /// <param name="page">The <see cref="EntityPage"/> used to page the elements.</param> |
| | | 39 | | /// <param name="interceptor">An interceptor to manipulate the generated page.</param> |
| | | 40 | | public static IQueryable<TEntity> Page<TEntity>(this IQueryable<TEntity> source, EntityPage page, IPageInterceptor? |
| | | 41 | | { |
| | 256 | 42 | | interceptor ??= IPageInterceptor.Default; |
| | | 43 | | |
| | 256 | 44 | | var result = interceptor?.Page(source, page); |
| | 256 | 45 | | if (result != null) |
| | 6 | 46 | | return result; |
| | | 47 | | |
| | 250 | 48 | | result = source; |
| | | 49 | | |
| | 250 | 50 | | var (skip, take) = page.GetSkipAndTake(); |
| | | 51 | | |
| | 195 | 52 | | if (skip is > 0) |
| | 63 | 53 | | result = result.Skip(skip.Value); |
| | | 54 | | |
| | 195 | 55 | | if (take is > 0) |
| | 195 | 56 | | result = result.Take(take.Value); |
| | | 57 | | |
| | 195 | 58 | | return result; |
| | | 59 | | } |
| | | 60 | | } |