mysql entity framework core pagination code example

Example 1: paging entity framework core

public static PagedResult GetPaged(this IQueryable query, 
                                         int page, int pageSize) where T : class
{
     var result = new PagedResult();
     result.CurrentPage = page;
     result.PageSize = pageSize;
     result.RowCount = query.Count();


     var pageCount = (double)result.RowCount / pageSize;
     result.PageCount = (int)Math.Ceiling(pageCount);
 
     var skip = (page - 1) * pageSize;     
     result.Results = query.Skip(skip).Take(pageSize).ToList();
 
     return result;
}

Example 2: paging entity framework core

public abstract class PagedResultBase
{
    public int CurrentPage { get; set; } 
    public int PageCount { get; set; } 
    public int PageSize { get; set; } 
    public int RowCount { get; set; }
 
    public int FirstRowOnPage
    {
 
        get { return (CurrentPage - 1) * PageSize + 1; }
    }
 
    public int LastRowOnPage
    {
        get { return Math.Min(CurrentPage * PageSize, RowCount); }
    }
}
 
public class PagedResult : PagedResultBase where T : class
{
    public IList Results { get; set; }
 
    public PagedResult()
    {
        Results = new List();
    }
}

Tags:

Misc Example