PagedUtil.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Furion.DependencyInjection;
  2. using Microsoft.EntityFrameworkCore;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace YBEE.EQM.Core
  9. {
  10. [SuppressSniffer]
  11. public static class PagedUtil
  12. {
  13. /// <summary>
  14. /// 分页拓展
  15. /// </summary>
  16. /// <typeparam name="TEntity"></typeparam>
  17. /// <param name="entities"></param>
  18. /// <param name="pageIndex">页码,必须大于0</param>
  19. /// <param name="pageSize"></param>
  20. /// <returns></returns>
  21. public static PageResult<TEntity> ToADPagedList<TEntity>(this IQueryable<TEntity> entities, int pageIndex = 1, int pageSize = 20)
  22. {
  23. if (pageIndex <= 0) throw new InvalidOperationException($"{nameof(pageIndex)} 必须是大于0的正整数。");
  24. var totalCount = entities.Count();
  25. var items = entities.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
  26. var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
  27. return new PageResult<TEntity>
  28. {
  29. PageIndex = pageIndex,
  30. PageSize = pageSize,
  31. Items = items,
  32. TotalCount = totalCount,
  33. };
  34. }
  35. /// <summary>
  36. /// 分页拓展
  37. /// </summary>
  38. /// <typeparam name="TEntity"></typeparam>
  39. /// <param name="entities"></param>
  40. /// <param name="pageIndex">页码,必须大于0</param>
  41. /// <param name="pageSize"></param>
  42. /// <param name="cancellationToken"></param>
  43. /// <returns></returns>
  44. public static async Task<PageResult<TEntity>> ToADPagedListAsync<TEntity>(this IQueryable<TEntity> entities, int pageIndex = 1, int pageSize = 20, CancellationToken cancellationToken = default)
  45. {
  46. if (pageIndex <= 0) throw new InvalidOperationException($"{nameof(pageIndex)} 必须是大于0的正整数。");
  47. var totalCount = await entities.CountAsync(cancellationToken);
  48. var items = await entities.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync(cancellationToken);
  49. var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
  50. return new PageResult<TEntity>
  51. {
  52. PageIndex = pageIndex,
  53. PageSize = pageSize,
  54. Items = items,
  55. TotalCount = totalCount,
  56. };
  57. }
  58. /// <summary>
  59. /// 分页拓展
  60. /// </summary>
  61. /// <typeparam name="TEntity"></typeparam>
  62. /// <param name="entities"></param>
  63. /// <param name="pageIndex">页码,必须大于0</param>
  64. /// <param name="pageSize"></param>
  65. /// <returns></returns>
  66. public static PageResult<TEntity> ToADPagedList<TEntity>(this IEnumerable<TEntity> entities, int pageIndex = 1, int pageSize = 20)
  67. {
  68. if (pageIndex <= 0) throw new InvalidOperationException($"{nameof(pageIndex)} 必须是大于0的正整数。");
  69. var totalCount = entities.Count();
  70. var items = entities.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
  71. var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
  72. return new PageResult<TEntity>
  73. {
  74. PageIndex = pageIndex,
  75. PageSize = pageSize,
  76. Items = items,
  77. TotalCount = totalCount,
  78. };
  79. }
  80. }
  81. }