平安校园
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

SqlSugarPageExtension.cs 6.6 KiB

4 miesięcy temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. namespace SafeCampus.SqlSugar;
  2. /// <summary>
  3. /// SqlSugar分页拓展类
  4. /// </summary>
  5. public static class SqlSugarPageExtension
  6. {
  7. /// <summary>
  8. /// SqlSugar分页扩展
  9. /// </summary>
  10. /// <typeparam name="TEntity"></typeparam>
  11. /// <param name="queryable"></param>
  12. /// <param name="pageNum"></param>
  13. /// <param name="pageSize"></param>
  14. /// <returns></returns>
  15. public static SqlSugarPagedList<TEntity> ToPagedList<TEntity>(this ISugarQueryable<TEntity> queryable, int pageNum, int pageSize)
  16. {
  17. var total = 0;
  18. var list = queryable.ToPageList(pageSize, pageNum, ref total);
  19. var pages = (int)Math.Ceiling(total / (double)pageSize);
  20. return new SqlSugarPagedList<TEntity>
  21. {
  22. PageNum = pageSize,
  23. PageSize = pageNum,
  24. List = list,
  25. Total = total,
  26. Pages = pages,
  27. HasNextPages = pageSize < pages,
  28. HasPrevPages = pageSize - 1 > 0
  29. };
  30. }
  31. /// <summary>
  32. /// SqlSugar分页扩展
  33. /// </summary>
  34. /// <typeparam name="TEntity"></typeparam>
  35. /// <param name="queryable"></param>
  36. /// <param name="pageNum"></param>
  37. /// <param name="pageSize"></param>
  38. /// <returns></returns>
  39. public static async Task<SqlSugarPagedList<TEntity>> ToPagedListAsync<TEntity>(this ISugarQueryable<TEntity> queryable, int pageNum, int pageSize)
  40. {
  41. RefAsync<int> totalCount = 0;
  42. var list = await queryable.ToPageListAsync(pageNum, pageSize, totalCount);
  43. var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
  44. return new SqlSugarPagedList<TEntity>
  45. {
  46. PageNum = pageNum,
  47. PageSize = pageSize,
  48. List = list,
  49. Total = (int)totalCount,
  50. Pages = totalPages,
  51. HasNextPages = pageNum < totalPages,
  52. HasPrevPages = pageNum - 1 > 0
  53. };
  54. }
  55. /// <summary>
  56. /// SqlSugar分页扩展
  57. /// </summary>
  58. /// <typeparam name="TEntity"></typeparam>
  59. /// <param name="queryable"></param>
  60. /// <param name="pageNum"></param>
  61. /// <param name="pageSize"></param>
  62. /// <returns></returns>
  63. public static async Task<SqlSugarPagedList<T1>> ToPagedListAsyncMapster<TEntity,T1>(this ISugarQueryable<TEntity> queryable, int pageNum, int pageSize)
  64. {
  65. RefAsync<int> totalCount = 0;
  66. var list = await queryable.ToPageListAsync(pageNum, pageSize, totalCount);
  67. var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
  68. return new SqlSugarPagedList<T1>
  69. {
  70. PageNum = pageNum,
  71. PageSize = pageSize,
  72. List = list.Adapt<List<T1>>(),
  73. Total = (int)totalCount,
  74. Pages = totalPages,
  75. HasNextPages = pageNum < totalPages,
  76. HasPrevPages = pageNum - 1 > 0
  77. };
  78. }
  79. /// <summary>
  80. /// SqlSugar分页扩展
  81. /// </summary>
  82. /// <typeparam name="TEntity"></typeparam>
  83. /// <typeparam name="TResult"></typeparam>
  84. /// <param name="queryable"></param>
  85. /// <param name="pageNum"></param>
  86. /// <param name="pageSize"></param>
  87. /// <param name="expression"></param>
  88. /// <returns></returns>
  89. public static SqlSugarPagedList<TResult> ToPagedList<TEntity, TResult>(this ISugarQueryable<TEntity> queryable, int pageNum, int pageSize,
  90. Expression<Func<TEntity, TResult>> expression)
  91. {
  92. var totalCount = 0;
  93. var items = queryable.ToPageList(pageNum, pageSize, ref totalCount, expression);
  94. var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
  95. return new SqlSugarPagedList<TResult>
  96. {
  97. PageNum = pageNum,
  98. PageSize = pageSize,
  99. List = items,
  100. Total = totalCount,
  101. Pages = totalPages,
  102. HasNextPages = pageNum < totalPages,
  103. HasPrevPages = pageNum - 1 > 0
  104. };
  105. }
  106. /// <summary>
  107. /// SqlSugar分页扩展
  108. /// </summary>
  109. /// <typeparam name="TEntity"></typeparam>
  110. /// <typeparam name="TResult"></typeparam>
  111. /// <param name="queryable"></param>
  112. /// <param name="pageNum"></param>
  113. /// <param name="pageSize"></param>
  114. /// <param name="expression"></param>
  115. /// <returns></returns>
  116. public static async Task<SqlSugarPagedList<TResult>> ToPagedListAsync<TEntity, TResult>(this ISugarQueryable<TEntity> queryable, int pageNum,
  117. int pageSize,
  118. Expression<Func<TEntity, TResult>> expression)
  119. {
  120. RefAsync<int> totalCount = 0;
  121. var items = await queryable.ToPageListAsync(pageNum, pageSize, totalCount, expression);
  122. var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
  123. return new SqlSugarPagedList<TResult>
  124. {
  125. PageNum = pageNum,
  126. PageSize = pageSize,
  127. List = items,
  128. Total = (int)totalCount,
  129. Pages = totalPages,
  130. HasNextPages = pageNum < totalPages,
  131. HasPrevPages = pageNum - 1 > 0
  132. };
  133. }
  134. /// <summary>
  135. /// 分页查询
  136. /// </summary>
  137. /// <typeparam name="T"></typeparam>
  138. /// <param name="list">数据列表</param>
  139. /// <param name="pageNum">当前页</param>
  140. /// <param name="pageSize">每页数量</param>
  141. /// <returns>分页集合</returns>
  142. public static LinqPagedList<T> LinqPagedList<T>(this List<T> list, int pageNum, int pageSize)
  143. {
  144. var result = list.ToPagedList(pageNum, pageSize);//获取分页
  145. //格式化
  146. return new LinqPagedList<T>
  147. {
  148. PageNum = pageNum,
  149. PageSize = result.PageSize,
  150. List = result.Data,
  151. Total = result.TotalCount,
  152. Pages = result.TotalPages,
  153. HasNextPages = result.HasNext,
  154. HasPrevPages = result.HasPrev
  155. };
  156. }
  157. }
  158. /// <summary>
  159. /// SqlSugar 分页泛型集合
  160. /// </summary>
  161. /// <typeparam name="TEntity"></typeparam>
  162. public class SqlSugarPagedList<TEntity>
  163. {
  164. /// <summary>
  165. /// 页码
  166. /// </summary>
  167. public int PageNum { get; set; }
  168. /// <summary>
  169. /// 数量
  170. /// </summary>
  171. public int PageSize { get; set; }
  172. /// <summary>
  173. /// 总条数
  174. /// </summary>
  175. public int Total { get; set; }
  176. /// <summary>
  177. /// 总页数
  178. /// </summary>
  179. public int Pages { get; set; }
  180. /// <summary>
  181. /// 当前页集合
  182. /// </summary>
  183. public IEnumerable<TEntity> List { get; set; }
  184. /// <summary>
  185. /// 是否有上一页
  186. /// </summary>
  187. public bool HasPrevPages { get; set; }
  188. /// <summary>
  189. /// 是否有下一页
  190. /// </summary>
  191. public bool HasNextPages { get; set; }
  192. }