| | 1 | | namespace Pozitron.QuerySpecification; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Provides extension methods for applying pagination to queryable and enumerable sources. |
| | 5 | | /// </summary> |
| | 6 | | public static class PaginationExtensions |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Applies pagination to the queryable source based on the specification. |
| | 10 | | /// </summary> |
| | 11 | | /// <typeparam name="T">The type of the entity.</typeparam> |
| | 12 | | /// <typeparam name="TResult">The type of the result.</typeparam> |
| | 13 | | /// <param name="source">The queryable source.</param> |
| | 14 | | /// <param name="specification">The specification containing pagination settings.</param> |
| | 15 | | /// <returns>The paginated queryable source.</returns> |
| | 16 | | public static IQueryable<TResult> ApplyPaging<T, TResult>(this IQueryable<TResult> source, Specification<T, TResult> |
| | 17 | | { |
| 17 | 18 | | var paging = specification.FirstOrDefault<SpecPaging>(ItemType.Paging); |
| 28 | 19 | | if (paging is null) return source; |
| | 20 | |
|
| 6 | 21 | | return ApplyPaging(source, paging.Skip, paging.Take); |
| | 22 | | } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Applies pagination to the enumerable source based on the specification. |
| | 26 | | /// </summary> |
| | 27 | | /// <typeparam name="T">The type of the entity.</typeparam> |
| | 28 | | /// <typeparam name="TResult">The type of the result.</typeparam> |
| | 29 | | /// <param name="source">The enumerable source.</param> |
| | 30 | | /// <param name="specification">The specification containing pagination settings.</param> |
| | 31 | | /// <returns>The paginated enumerable source.</returns> |
| | 32 | | public static IEnumerable<TResult> ApplyPaging<T, TResult>(this IEnumerable<TResult> source, Specification<T, TResul |
| | 33 | | { |
| 6 | 34 | | var paging = specification.FirstOrDefault<SpecPaging>(ItemType.Paging); |
| 7 | 35 | | if (paging is null) return source; |
| | 36 | |
|
| 5 | 37 | | return ApplyPaging(source, paging.Skip, paging.Take); |
| | 38 | | } |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Applies pagination to the queryable source based on the pagination settings. |
| | 42 | | /// </summary> |
| | 43 | | /// <typeparam name="T">The type of the entity.</typeparam> |
| | 44 | | /// <param name="source">The queryable source.</param> |
| | 45 | | /// <param name="pagination">The pagination settings.</param> |
| | 46 | | /// <returns>The paginated queryable source.</returns> |
| | 47 | | public static IQueryable<T> ApplyPaging<T>(this IQueryable<T> source, Pagination pagination) |
| 6 | 48 | | => ApplyPaging(source, pagination.Skip, pagination.Take); |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Applies pagination to the queryable source based on the specification. |
| | 52 | | /// </summary> |
| | 53 | | /// <typeparam name="T">The type of the entity.</typeparam> |
| | 54 | | /// <param name="source">The queryable source.</param> |
| | 55 | | /// <param name="specification">The specification containing pagination settings.</param> |
| | 56 | | /// <returns>The paginated queryable source.</returns> |
| | 57 | | public static IQueryable<T> ApplyPaging<T>(this IQueryable<T> source, Specification<T> specification) |
| | 58 | | { |
| 24 | 59 | | var paging = specification.FirstOrDefault<SpecPaging>(ItemType.Paging); |
| 40 | 60 | | if (paging is null) return source; |
| | 61 | |
|
| 8 | 62 | | return ApplyPaging(source, paging.Skip, paging.Take); |
| | 63 | | } |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// Applies pagination to the enumerable source based on the specification. |
| | 67 | | /// </summary> |
| | 68 | | /// <typeparam name="T">The type of the entity.</typeparam> |
| | 69 | | /// <param name="source">The enumerable source.</param> |
| | 70 | | /// <param name="specification">The specification containing pagination settings.</param> |
| | 71 | | /// <returns>The paginated enumerable source.</returns> |
| | 72 | | public static IEnumerable<T> ApplyPaging<T>(this IEnumerable<T> source, Specification<T> specification) |
| | 73 | | { |
| 6 | 74 | | var paging = specification.FirstOrDefault<SpecPaging>(ItemType.Paging); |
| 7 | 75 | | if (paging is null) return source; |
| | 76 | |
|
| 5 | 77 | | return ApplyPaging(source, paging.Skip, paging.Take); |
| | 78 | | } |
| | 79 | |
|
| | 80 | | private static IQueryable<T> ApplyPaging<T>(this IQueryable<T> source, int skip, int take) |
| | 81 | | { |
| | 82 | | // If skip is 0, avoid adding to the IQueryable. It will generate more optimized SQL that way. |
| 20 | 83 | | if (skip > 0) |
| | 84 | | { |
| 17 | 85 | | source = source.Skip(skip); |
| | 86 | | } |
| | 87 | |
|
| 20 | 88 | | if (take >= 0) |
| | 89 | | { |
| 18 | 90 | | source = source.Take(take); |
| | 91 | | } |
| | 92 | |
|
| 20 | 93 | | return source; |
| | 94 | | } |
| | 95 | |
|
| | 96 | | private static IEnumerable<T> ApplyPaging<T>(this IEnumerable<T> source, int skip, int take) |
| | 97 | | { |
| 10 | 98 | | if (skip > 0) |
| | 99 | | { |
| 8 | 100 | | source = source.Skip(skip); |
| | 101 | | } |
| | 102 | |
|
| 10 | 103 | | if (take >= 0) |
| | 104 | | { |
| 8 | 105 | | source = source.Take(take); |
| | 106 | | } |
| | 107 | |
|
| 10 | 108 | | return source; |
| | 109 | | } |
| | 110 | | } |