I created a nuget library that does pagination for you. https://github.com/wdunn001/EntityFrameworkPaginateCore
add nuget to project
Install-Package EntityFrameworkPaginateCore
add
using EntityFrameworkPaginateCore;
to you provider
Has 1 method and 2 overloads for that method overloads allow sorting and filtering. use the sort object and the filter objects
public async Task<Page<Example>> GetPaginatedExample(
int pageSize = 10,
int currentPage = 1,
string searchText = "",
int sortBy = 2
)
{
var filters = new Filters<Example>();
filters.Add(!string.IsNullOrEmpty(searchText), x => x.Title.Contains(searchText));
var sorts = new Sorts<Example>();
sorts.Add(sortBy == 1, x => x.ExampleId);
sorts.Add(sortBy == 2, x => x.Edited);
sorts.Add(sortBy == 3, x => x.Title);
try
{
return await _Context.EfExample.Select(e => _mapper.Map<Example>(e)).PaginateAsync(currentPage, pageSize, sorts, filters);
}
catch (Exception ex)
{
throw new KeyNotFoundException(ex.Message);
}
}
select count() from tablesql statement - so while you do need 2 queries, one of them is very cheap.select count()is not cheap at all! Actually it has almost the same complexity as actual fetching of data, the only difference is instead of fetching rows it only counts them. But it still have to perform all scans, etc.