EntityFramework query extension for getting page data from database for views
Install the UN.EntityFramework.Extensions.Pagination package from NuGet:
Install-Package UN.EntityFramework.Extensions.Pagination
TakePageResult method is the IQueryable extension method. which adds the skip and take to your IQueryable Query. It can be used with your any generic list or entity framework query.
PagedResult is the generic class which have the 4 paramaters which you can use to return data its really up to you if you would want to use this class.
IPageQuery is a interface which holds all the parameter required for pagination which must be passed in your query.
Example 1
public asyc Task<PagedResult<YourModel>> GetMyModelsByPage(IPageQuery pageQuery)
{
var result = await dbcontext.Table
.TakePageResult(pageQuery).ToArrayAsync();
var totalItems = result.Count;
return new PagedResult<YourModel>
{
TotalItems = totalItems,
PageSize = pageQuery.PageSize,
PageNr = pageQuery.PageNr,
Result = result
};
}
Example 2 Get all the list of persons based on name from your database
public class MySearchQuery : IPageQuery
{
public string Name { get ; set ; }
public int PageSize { get ; set ; }
public int PageNr { get ; set ; }
}
public asyc Task<PagedResult<Person>> GetPersonsByNameAndPageResult(MySearchQuery searchQuery)
{
var result = await dbcontext.Persons.Where(w => x.Name == searchQuery.Name)
.TakePageResult(searchQuery).ToArrayAsync();
var totalItems = result.Count;
return new PagedResult<Person>
{
TotalItems = totalItems,
PageSize = searchQuery.PageSize,
PageNr = searchQuery.PageNr,
Result = result
};
}
Find the sample running application here