SqlSugarRepositoryExtensions.cs 1.4 KB

12345678910111213141516171819202122232425262728293031
  1. using Exam.Share.Requests;
  2. using SqlSugar;
  3. namespace Exam.Repository.Sqlsugar.Extensions
  4. {
  5. public static class SqlSugarRepositoryExtensions
  6. {
  7. public static async Task<(int Total, List<TEntity> Items)> ToPagedListAsync<TEntity>(this ISugarQueryable<TEntity> query, int pageIndex, int pageSize, CancellationToken cancellationToken = default)
  8. where TEntity : class, new()
  9. {
  10. RefAsync<int> total = 0;
  11. var items = await query.ToPageListAsync(pageIndex, pageSize, total);
  12. return (total.Value, items);
  13. }
  14. public static async Task<(int Total, List<TEntity> Items)> ToPagedListAsync<TEntity>(this ISugarQueryable<TEntity> query, PagedRequest dto, CancellationToken cancellationToken = default)
  15. where TEntity : class, new()
  16. {
  17. RefAsync<int> total = 0;
  18. var items = await query.ToPageListAsync(dto.PageIndex, dto.PageSize, total);
  19. return (total.Value, items);
  20. }
  21. public static Task<List<TEntity>> ToFixedListAsync<TEntity>(this ISugarQueryable<TEntity> query, int queryIndex, int? queryCount = null, CancellationToken cancellationToken = default)
  22. where TEntity : class, new()
  23. {
  24. if (queryCount is null or 0) queryCount = 50;
  25. return query.Skip(queryIndex * queryCount.Value).Take(queryCount.Value).ToListAsync(cancellationToken);
  26. }
  27. }
  28. }