I have a DbContext with a lot of DbSets. Every DbSet should have a function to get a page of items from the set, with a given pageSize and ordered by a specific sortOrder. Something like:
var pageItems = dbContext.Posts
.Where(post => post.BlogId == blogId)
.OrderBy(some sortorder)
.Skip(pageNr * pageSize)
.Take(pageSize);
I want to be able to do this with all my DbSets, so I have created an extension method where one of the parameters specifies the foreign key to compare and another the value this foreign key should have.
public static IQueryable<TSource> GetPage<TSource>(this IQueryable<TSource> source,
int pageNr, int pageSize,
Expression<Func<TSource, Tproperty>> keySelector, Tproperty comparisonValue)
{
return source
.Where( ??? )
.OrderBy(some sortorder)
.Skip(pageNr * pageSize)
.Take(pageSize);
}
How to convert the keySelector in a predicate suitable for Where?