开发者

How to pass in a LINQ statement as a lambda

开发者 https://www.devze.com 2023-04-04 05:46 出处:网络
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 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);
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消