phronCare/Models/Helpers/PaginationExtensions.cs

34 lines
849 B
C#
Raw Normal View History

2025-04-06 01:19:47 -03:00
using Domain.Generics;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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
};
}
}
}