SqlSugarPagedList.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. namespace Admin.NET.Core;
  2. /// <summary>
  3. /// 分页泛型集合
  4. /// </summary>
  5. /// <typeparam name="TEntity"></typeparam>
  6. public class SqlSugarPagedList<TEntity>
  7. where TEntity : new()
  8. {
  9. /// <summary>
  10. /// 页码
  11. /// </summary>
  12. public int Page { get; set; }
  13. /// <summary>
  14. /// 页容量
  15. /// </summary>
  16. public int PageSize { get; set; }
  17. /// <summary>
  18. /// 总条数
  19. /// </summary>
  20. public int Total { get; set; }
  21. /// <summary>
  22. /// 总页数
  23. /// </summary>
  24. public int TotalPages { get; set; }
  25. /// <summary>
  26. /// 当前页集合
  27. /// </summary>
  28. public IEnumerable<TEntity> Items { get; set; }
  29. /// <summary>
  30. /// 是否有上一页
  31. /// </summary>
  32. public bool HasPrevPage { get; set; }
  33. /// <summary>
  34. /// 是否有下一页
  35. /// </summary>
  36. public bool HasNextPage { get; set; }
  37. }
  38. /// <summary>
  39. /// 分页拓展类
  40. /// </summary>
  41. public static class SqlSugarPagedExtensions
  42. {
  43. /// <summary>
  44. /// 分页拓展
  45. /// </summary>
  46. /// <param name="query">ISugarQueryable 对象</param>
  47. /// <param name="pageIndex">当前页码,从1开始</param>
  48. /// <param name="pageSize">页码容量</param>
  49. /// <param name="expression">查询结果 Select 表达式</param>
  50. /// <returns></returns>
  51. public static SqlSugarPagedList<TResult> ToPagedList<TEntity, TResult>(this ISugarQueryable<TEntity> query, int pageIndex, int pageSize,
  52. Expression<Func<TEntity, TResult>> expression)
  53. where TEntity : new()
  54. where TResult : new()
  55. {
  56. var total = 0;
  57. var items = query.ToPageList(pageIndex, pageSize, ref total, expression);
  58. var totalPages = pageSize > 0 ? (int)Math.Ceiling(total / (double)pageSize) : 0;
  59. return new SqlSugarPagedList<TResult>
  60. {
  61. Page = pageIndex,
  62. PageSize = pageSize,
  63. Items = items,
  64. Total = total,
  65. TotalPages = totalPages,
  66. HasNextPage = pageIndex < totalPages,
  67. HasPrevPage = pageIndex - 1 > 0
  68. };
  69. }
  70. /// <summary>
  71. /// 分页拓展
  72. /// </summary>
  73. /// <param name="query">ISugarQueryable 对象</param>
  74. /// <param name="pageIndex">当前页码,从1开始</param>
  75. /// <param name="pageSize">页码容量</param>
  76. /// <returns></returns>
  77. public static SqlSugarPagedList<TEntity> ToPagedList<TEntity>(this ISugarQueryable<TEntity> query, int pageIndex, int pageSize)
  78. where TEntity : new()
  79. {
  80. return ToPagedList(query, pageIndex, pageSize, u => u);
  81. }
  82. /// <summary>
  83. /// 分页拓展
  84. /// </summary>
  85. /// <param name="query">ISugarQueryable 对象</param>
  86. /// <param name="pageIndex">当前页码,从1开始</param>
  87. /// <param name="pageSize">页码容量</param>
  88. /// <param name="expression">查询结果 Select 表达式</param>
  89. /// <returns></returns>
  90. public static async Task<SqlSugarPagedList<TResult>> ToPagedListAsync<TEntity, TResult>(this ISugarQueryable<TEntity> query, int pageIndex, int pageSize,
  91. Expression<Func<TEntity, TResult>> expression)
  92. where TEntity : new()
  93. where TResult : new()
  94. {
  95. RefAsync<int> total = 0;
  96. var items = await query.ToPageListAsync(pageIndex, pageSize, total, expression);
  97. var totalPages = pageSize > 0 ? (int)Math.Ceiling(total / (double)pageSize) : 0;
  98. return new SqlSugarPagedList<TResult>
  99. {
  100. Page = pageIndex,
  101. PageSize = pageSize,
  102. Items = items,
  103. Total = total,
  104. TotalPages = totalPages,
  105. HasNextPage = pageIndex < totalPages,
  106. HasPrevPage = pageIndex - 1 > 0
  107. };
  108. }
  109. /// <summary>
  110. /// 分页拓展
  111. /// </summary>
  112. /// <param name="query">ISugarQueryable 对象</param>
  113. /// <param name="pageIndex">当前页码,从1开始</param>
  114. /// <param name="pageSize">页码容量</param>
  115. /// <returns></returns>
  116. public static Task<SqlSugarPagedList<TEntity>> ToPagedListAsync<TEntity>(this ISugarQueryable<TEntity> query, int pageIndex, int pageSize)
  117. where TEntity : new()
  118. {
  119. return ToPagedListAsync(query, pageIndex, pageSize, u => u);
  120. }
  121. /// <summary>
  122. /// 分页拓展
  123. /// </summary>
  124. /// <param name="list">集合对象</param>
  125. /// <param name="pageIndex">当前页码,从1开始</param>
  126. /// <param name="pageSize">页码容量</param>
  127. /// <returns></returns>
  128. public static SqlSugarPagedList<TEntity> ToPagedList<TEntity>(this IEnumerable<TEntity> list, int pageIndex, int pageSize)
  129. where TEntity : new()
  130. {
  131. var total = list.Count();
  132. var items = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
  133. var totalPages = pageSize > 0 ? (int)Math.Ceiling(total / (double)pageSize) : 0;
  134. return new SqlSugarPagedList<TEntity>
  135. {
  136. Page = pageIndex,
  137. PageSize = pageSize,
  138. Items = items,
  139. Total = total,
  140. TotalPages = totalPages,
  141. HasNextPage = pageIndex < totalPages,
  142. HasPrevPage = pageIndex - 1 > 0
  143. };
  144. }
  145. }