using Bunny.Dao.Common.Result; using Microsoft.EntityFrameworkCore; namespace Bunny.Common.Context.Database; public static class GetPagedResultAsync { public static async Task> GetPagedListAsync(IQueryable source, int page, int limit) { var total = await source.CountAsync(); IEnumerable items = await source.Skip((page - 1) * limit).Take(limit).ToListAsync(); return new PageResult(page, limit, total, items); } public static async Task> GetPagedListFromSqlAsync(DbContext dbContext, string sql, string table, int page, int limit) where T : class { // 计算SQL查询的总数 var total = await dbContext.Set().CountAsync(); // 执行分页查询 var paginatedSql = $"{sql} ORDER BY (SELECT NULL) OFFSET {(page - 1) * limit} ROWS FETCH NEXT {limit} ROWS ONLY"; IEnumerable items = await dbContext.Set().FromSqlRaw(paginatedSql).ToListAsync(); return new PageResult(page, limit, total, items); } }