A very simple pagination library that takes an IQueryable<T>
and returns a paginated result. Can be used both as extension methods or through DI.
For MongoDb, install SimplePagination.MongoDb, which enables better pagination for IMongoQueryable<T>
.
Use Visual Studio Package Manager or
Install-Package SimplePaginator
Have a look at the tests for more examples.
var source = Enumerable.Range(1,100).AsQueryable();
//Returns an IPaginationResult with the first 15 entries, where the entry count (and in turn, the page count) is calculated using LINQ's count() function
IPaginationResult paginated = source.Paginate(page: 1, pageSize: 15);
//paginated.Page == 1
//paginated.PageCount == 7
//paginated.PageSize == 15
//paginated.EntriesCount == 100
Use a custom count function for returning number of entries. The idea is to use this when LINQ's default Count()
function isn't what you're looking for.
var source = Enumerable.Range(1,100).AsQueryable();
//Returns an IPaginationResult with the first 15 entries, where the entry count (and in turn, the page count) is calculated using a custom function.
IPaginationResult paginated = source.Paginate(page: 1, pageSize: 15, (q) => 50);
//paginated.Page == 1
//paginated.PageCount == 4
//paginated.PageSize == 15
//paginated.EntriesCount == 50
You can also use an async custom function
//Returns an IPaginationResult with the first 15 entries, where the entry count (and in turn, the page count) is calculated using a custom async function.
IPaginationResult paginated = await source.PaginateAsync(page: 1, pageSize: 15, (q) => Task.FromResult(50));
//paginated.Page == 1
//paginated.PageCount == 4
//paginated.PageSize == 15
//paginated.EntriesCount == 50
Behind the scenes, the default service simply calls the extensions methods so there's no practical difference.
Startup.cs
services.AddSimplePaginator();
//Injected through DI
public void Example(IPaginationService pagination) {
var source = Enumerable.Range(1,100).AsQueryable();
//Returns an IPaginationResult with the first 15 entries, where the entry count (and in turn, the page count) is calculated using LINQ's count() function
IPaginationResult paginated = pagination.Paginate(query: source, page: 1, pageSize: 15);
//paginated.Page == 1
//paginated.PageCount == 7
//paginated.PageSize == 15
//paginated.EntriesCount == 100
}
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.