✨
This commit is contained in:
32
StopShopping.Services/Models/Resp/PagedResult.cs
Normal file
32
StopShopping.Services/Models/Resp/PagedResult.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user