I have a paging class that includes an ApplyFilter()
method, which filters rows for the current page. Since paging generally only makes sense in sorted results, this method accepts and returns an IOrderedQueryable<T>
(instead of just a IQueryable<T>
).
I use it like this:
var query = DbContext.Items
.Where(i => i.Value > 0)
.OrderBy(i => i.SortColumn);
query = pagination.ApplyFilter(query);
But there's a problem with my ApplyFilter()
method.
public IOrderedQueryable<T> ApplyFilter<T>(IOrderedQueryable<T> query)
{
return query.Skip((CurrentPage - 1) * PageSize).Take(PageSize);
}
The Skip()
and Take()
methods both return an IQueryable<T>
, so I get an error that the return value cannot be converted from IQueryable<T>
to IOrderedQueryable<T>
.
How can I use Skip()
and Take()
and still have an IOrderedQueryable<T>
?