Add a Linq clause to a Iqueryable object
I have a method who get a Iqueryable object get fr开发者_运维问答om a LINQ Query On this object, i want to add a LINQ clause : .skip(20) How can i do that from my method ?
Thanks by advance
You can't "add" a clause to an existing query, because the queries themselves are immutable - but you can create a new query which is the existing one but with an extra clause:
IQueryable<Foo> newQuery = oldQuery.Skip(20);
var query = FunctionThatReturnsIQueryable().Skip(20);
As long as you haven't enumerated over the query, you can always add clauses. The clauses are only executed as soon as you get the results (e.g. with for each
).
You could do something like GetSomeIQueryable().Skip(20)
. That returns you a new query that includes your skip clause.
精彩评论