0
votes

I want to use one class for all my site paginations,And because I'm using the interface, I do not know how to change it,

Pagination Class :

public class Pagination<T> : List<T>
{
    public int PageIndex { get; private set; }
    public int TotalPages { get;  set; }
    public Pagination(List<T>items,int count,int pageIndex,int pageSize)
    {
        PageIndex = pageIndex;
        TotalPages = (int)Math.Ceiling(count / (double)pageSize);
        this.AddRange(items);
    }
    public bool PreviousPage
    {
        get
        {
            return (PageIndex > 1);
        }
    }
    public bool NextPage
    {
        get
        {
            return (PageIndex < TotalPages);
        }
    }
    public static async Task<Pagination<T>> CreateAsync(IQueryable<T> source,int pageIndex,int pageSize)
    {
        var count = await source.CountAsync();
        var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
        return new Pagination<T>(items, count, pageIndex, pageSize);
    }
}

Interface Query :

public async Task<List<Product>> GetProductsWithPage(int page)
    {
       var query =  _context.Products.Where(p => p.IsShowProduct == true && !p.Amazing).Include(c => c.Category).Include(p => p.ProductDetail).OrderByDescending(p => p.ProductDetail.DateProduct);                
           
       return await Pagination<Product>.CreateAsync(query, page, 3); ///Pagination
    }

Controller :

 public async Task<IActionResult> Index(int page = 1)
{
    return await _admin.GetProductsWithPage(page);
}
I do not know how to change itWhat you want to change?CreateAsync(IQueryable<T> source,int pageIndex,int pageSize) not work?Yiyi You