Paging made easy with Linq syntax and extensions. Translates directly to SQL for database-side paging when using Entity Framework.
Install-Package LinqPaging
dotnet add package LinqPaging
Just call ToPagedList
on your Linq expression to convert the source into a subset. When using Entity Framework, the extension will be translated to SQL using the Skip
and Take
methods for you.
// defaults to getting all records
var employees = dbContext.Employees.ToPagedList();
// page 1, 30 results per page
var employees = dbContext.Employees.ToPagedList(1, 30);
You can include the IPageable interface to a strongly-typed request class and pass it to the ToPagedList
method.
public class GetEmployeesRequest : IPageable
{
public string Query { get; set; }
public int? Page { get; set; }
public int? Results { get; set; }
}
// employees?query={query}&page={page}&results={results}
[Route("employees")]
public async Task<IActionResult> GetEmployeesRequest([FromQuery]GetEmployeesRequest request)
{
return dbContext.Employees
.Where(e=> e.Name.Contains(request.Query))
.ToPagedList(request);
}