phronCare/Models/Helpers/PaginationExtensions.cs

28 lines
732 B
C#
Raw Permalink Normal View History

2025-04-06 01:19:47 -03:00
using Domain.Generics;
using Microsoft.EntityFrameworkCore;
namespace Models.Helpers
{
public static class PaginationExtensions
{
public static async Task<PagedResult<T>> ToPagedResultAsync<T>(
this IQueryable<T> query,
int page,
int pageSize)
{
var totalItems = await query.CountAsync();
var items = await query
.Skip((page - 1) * pageSize)
.Take(pageSize)
.ToListAsync();
return new PagedResult<T>
{
Items = items,
TotalItems = totalItems,
Page = page,
PageSize = pageSize
};
}
}
}