|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Data.Entity; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
| 7 | + |
| 8 | +namespace X.PagedList.EntityFramework; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// EntityFramework extension methods designed to simplify the creation of instances of <see cref="PagedList{T}"/>. |
| 12 | +/// </summary> |
| 13 | +public static class PagedListExtensions |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Async creates a subset of this collection of objects that can be individually accessed by index and |
| 17 | + /// containing metadata about the collection of objects the subset was created from. |
| 18 | + /// </summary> |
| 19 | + /// <typeparam name="T">The type of object the collection should contain.</typeparam> |
| 20 | + /// <param name="superset">The collection of objects to be divided into subsets. If the collection implements <see cref="IQueryable{T}"/>, it will be treated as such.</param> |
| 21 | + /// <param name="pageNumber">The one-based index of the subset of objects to be contained by this instance.</param> |
| 22 | + /// <param name="pageSize">The maximum size of any individual subset.</param> |
| 23 | + /// <param name="totalSetCount">The total size of set</param> |
| 24 | + /// <param name="cancellationToken"></param> |
| 25 | + /// <returns> |
| 26 | + /// A subset of this collection of objects that can be individually accessed by index and containing metadata |
| 27 | + /// about the collection of objects the subset was created from. |
| 28 | + /// </returns> |
| 29 | + /// <seealso cref="PagedList{T}"/> |
| 30 | + public static async Task<IPagedList<T>> ToPagedListAsync<T>(this IQueryable<T> superset, int pageNumber, int pageSize, int? totalSetCount, CancellationToken cancellationToken) |
| 31 | + { |
| 32 | + if (superset == null) |
| 33 | + { |
| 34 | + throw new ArgumentNullException(nameof(superset)); |
| 35 | + } |
| 36 | + |
| 37 | + if (pageNumber < 1) |
| 38 | + { |
| 39 | + throw new ArgumentOutOfRangeException($"pageNumber = {pageNumber}. PageNumber cannot be below 1."); |
| 40 | + } |
| 41 | + |
| 42 | + if (pageSize < 1) |
| 43 | + { |
| 44 | + throw new ArgumentOutOfRangeException($"pageSize = {pageSize}. PageSize cannot be less than 1."); |
| 45 | + } |
| 46 | + |
| 47 | + List<T> subset; |
| 48 | + int totalCount; |
| 49 | + |
| 50 | + if (totalSetCount.HasValue) |
| 51 | + { |
| 52 | + totalCount = totalSetCount.Value; |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + totalCount = await superset |
| 57 | + .CountAsync(cancellationToken: cancellationToken) |
| 58 | + .ConfigureAwait(false); |
| 59 | + } |
| 60 | + |
| 61 | + if (totalCount > 0) |
| 62 | + { |
| 63 | + int skip = (pageNumber - 1) * pageSize; |
| 64 | + |
| 65 | + subset = await superset |
| 66 | + .Skip(skip) |
| 67 | + .Take(pageSize) |
| 68 | + .ToListAsync(cancellationToken) |
| 69 | + .ConfigureAwait(false); |
| 70 | + } |
| 71 | + else |
| 72 | + { |
| 73 | + subset = new List<T>(); |
| 74 | + } |
| 75 | + |
| 76 | + return new StaticPagedList<T>(subset, pageNumber, pageSize, totalCount); |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Async creates a subset of this collection of objects that can be individually accessed by index and |
| 81 | + /// containing metadata about the collection of objects the subset was created from. |
| 82 | + /// </summary> |
| 83 | + /// <typeparam name="T">The type of object the collection should contain.</typeparam> |
| 84 | + /// <param name="superset">The collection of objects to be divided into subsets. If the collection implements <see cref="IQueryable{T}"/>, it will be treated as such.</param> |
| 85 | + /// <param name="pageNumber">The one-based index of the subset of objects to be contained by this instance.</param> |
| 86 | + /// <param name="pageSize">The maximum size of any individual subset.</param> |
| 87 | + /// <param name="totalSetCount">The total size of set</param> |
| 88 | + /// <returns> |
| 89 | + /// A subset of this collection of objects that can be individually accessed by index and containing metadata |
| 90 | + /// about the collection of objects the subset was created from. |
| 91 | + /// </returns> |
| 92 | + /// <seealso cref="PagedList{T}"/> |
| 93 | + public static Task<IPagedList<T>> ToPagedListAsync<T>(this IQueryable<T> superset, int pageNumber, int pageSize, int? totalSetCount = null) |
| 94 | + { |
| 95 | + return ToPagedListAsync(superset, pageNumber, pageSize, totalSetCount, CancellationToken.None); |
| 96 | + } |
| 97 | +} |
0 commit comments