Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using more than one condition in linq's where method

Tags:

c#

I have a line of code using where:

codebase.Methods.Where(x => x.Body.Scopes.Count > 5); 

How can I insert more than one condition? So I can say x => predicate && y => predicate?

Thanks

like image 392
GurdeepS Avatar asked Feb 17 '10 13:02

GurdeepS


People also ask

Can we use Multiple where clause in LINQ?

Well, you can just put multiple "where" clauses in directly, but I don't think you want to. Multiple "where" clauses ends up with a more restrictive filter - I think you want a less restrictive one.

Can we have multiple conditions for the ON clause?

You can specify multiple conditions in a single WHERE clause to, say, retrieve rows based on the values in multiple columns. You can use the AND and OR operators to combine two or more conditions into a compound condition. AND, OR, and a third operator, NOT, are logical operators.


1 Answers

You can roll your separate conditions into a single predicate if you like:

codebase.Methods.Where(x => (x.Body.Scopes.Count > 5) && (x.Foo == "test")); 

Or you can use a separate Where call for each condition:

codebase.Methods.Where(x => x.Body.Scopes.Count > 5)                 .Where(x => x.Foo == "test"); 
like image 185
LukeH Avatar answered Oct 12 '22 21:10

LukeH