This library provides an easy-to-use extension for paginating IQueryable<T> objects in Entity Framework Core, allowing
for efficient, dynamic pagination of database queries.
To install the library, use the following NuGet command:
dotnet add package BenBristow.EntityFrameworkCore.Pagination-
Reference the library in your project by adding
using BenBristow.EntityFrameworkCore.Pagination.Extensions;. -
Paginate your query by calling
ToPaginationResultAsyncon anyIOrderedQueryable<T>object, specifying the desired page and page size.
Example:
var paginatedResults = await myDbContext.MyEntities
.OrderBy(entity => entity.Id)
.ToPaginationResultAsync(page: 1, pageSize: 10);You can also use projection to select specific fields or transform the results while paginating:
var paginatedResults = await myDbContext.MyEntities
.OrderBy(entity => entity.Id)
.ToPaginationResultAsync(
entity => new MyDto { Id = entity.Id, Name = entity.Name },
page: 1,
pageSize: 10
);Paginates the source IQueryable<T> based on the provided parameters.
source: The orderedIQueryable<T>source to paginate.page: The page number to retrieve. Defaults to 1.pageSize: The number of items per page. Can benullto return all items.cancellationToken: A token to observe while waiting for the task to complete.
A Task<PaginationResult<T>> representing the asynchronous operation, containing the paginated results.
Paginates the source IQueryable<TSource> and projects the results to TResult based on the provided parameters.
source: The orderedIQueryable<TSource>source to paginate.projection: A function to map each element ofTSourcetoTResult.page: The page number to retrieve. Defaults to 1.pageSize: The number of items per page. Can benullto return all items.cancellationToken: A token to observe while waiting for the task to complete.
A Task<PaginationResult<TResult>> representing the asynchronous operation, containing the paginated and projected results.
Creates an empty PaginationResult<T> with sensible defaults. Useful when there are no results to display (e.g., an empty search) and you still want to return a valid pagination shape without performing a query.
pageSize(optional): The number of items per page. If provided,PageSizeis set to this value; if omitted,PageSizeisnull.
An empty PaginationResult<T> with the following defaults:
Results: empty collectionTotalCount:0Page:1PageCount:1PageSize:null(or the suppliedpageSize)
// Return an empty result (no page size)
return PaginationResult<MyDto>.Empty();
// Return an empty result but preserve a desired page size in the response
return PaginationResult<MyDto>.Empty(pageSize: 25);public class MyEntityService
{
private readonly MyDbContext _context;
public MyEntityService(MyDbContext context)
{
_context = context;
}
/// <summary>
/// Asynchronously gets paginated entities.
/// </summary>
/// <param name="page">The page number to retrieve, starting from 1.</param>
/// <param name="pageSize">The number of items per page.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the paginated results.</returns>
public async Task<PaginationResult<MyEntity>> GetPaginatedEntities(int page = 1, int pageSize = 10)
{
var paginatedResults = await _context.Entities
.OrderBy(e => e.Id)
.ToPaginationResultAsync(page, pageSize);
return paginatedResults;
}
/// <summary>
/// Asynchronously gets paginated entities with projection to DTOs.
/// </summary>
/// <param name="page">The page number to retrieve, starting from 1.</param>
/// <param name="pageSize">The number of items per page.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the paginated and projected results.</returns>
public async Task<PaginationResult<MyEntityDto>> GetPaginatedEntityDtos(int page = 1, int pageSize = 10)
{
var paginatedResults = await _context.Entities
.OrderBy(e => e.Id)
.ToPaginationResultAsync(
e => new MyEntityDto { Id = e.Id, Name = e.Name },
page,
pageSize
);
return paginatedResults;
}
}Contributions are welcome! Please feel free to submit a pull request or open an issue for any bugs or feature requests.
This project is licenced under the GNU GPL v3 or later license.