Im hoping this isnt too basic a question- I have looked at similar questions but cant seem to understand, so Im reaching out for help.
Im using a repository pattern that I want to make generic - here is what I have for the generic:
static public IQueryable<T> Get(Func<IQueryable<T>> pred, uint page=0, uint pageSize=开发者_如何学运维10)
{
return pred()
.Skip((int)(page * pageSize))
.Take((int)pageSize);
}
So I want to call it, but get an "has invalid arguments" with whatever lambda I try.
If I declare a method that returns an IQueryable and pass it in as the first parameter, that works- no compile error. Im stumped.
Please help? what is the correct way to call this with a lambda? Or, if my generic is out of whack, how best to declare it? I assumed a Func that returns an IQueryable would be best...
Typically, rather taking in a Func<IQueryable<T>>,
you just take the IQueryable<T>
and work with that directly.
static public IQueryable<T> Get<T>(IQueryable<T> source, uint page=0, uint pageSize=10)
{
return source
.Skip((int)(page * pageSize))
.Take((int)pageSize);
}
Get is missing the T generic
static public IQueryable<T> Get<T>(Func<IQueryable<T>> pred, uint page = 0, uint pageSize = 10)
{
return pred()
.Skip((int)(page * pageSize))
.Take((int)pageSize);
}
精彩评论