Files
StopShopping/StopShopping.Services/Models/Resp/PagedResult.cs
2026-03-25 14:55:34 +08:00

33 lines
879 B
C#

namespace StopShopping.Services.Models.Resp;
public class PagedResult<T>
{
public int PageCount { get; set; }
public int PageSize { get; set; }
public int PageIndex { get; set; }
public List<T> Data { get; set; } = [];
}
public static class PagedQueryExtensions
{
public static async Task<PagedResult<T>> ToPagedAsync<T>(this IAsyncEnumerable<T> query
, int pageIndex = 1
, int pageSize = 20)
{
PagedResult<T> result = new()
{
PageSize = pageSize,
PageIndex = pageIndex
};
var total = await query.CountAsync();
result.PageCount = (int)Math.Ceiling(total / (decimal)result.PageSize);
result.Data = await query
.Skip((result.PageIndex - 1) * result.PageSize)
.Take(result.PageSize)
.ToListAsync();
return result;
}
}