Indeed, you are doing a ToList()
, so the query will be executed, and deferred execution won't be deferred anymore.
You can try it without the ToList()
call, but it all depends on the implementation of the ToPagedList(int, int)
method.
You can just do the paging yourself.
const int pageSize = 25;
const int pageNumber = 1;
IQueryable<Product> query = _dbContext.Products;
var pagedQuery = query.Skip(pageSize * (pageNumber - 1)).Take(pageSize).ToList();
And yes, you can just hide this logic behind an extension method on IQueryable<T>
.
public static class IQueryableExtensions {
public static IQueryable<T> ToPagedQuery<T>(this IQueryable<T> query, int pageSize, int pageNumber) {
return query.Skip(pageSize * (pageNumber - 1)).Take(pageSize);
}
}