58

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

0

7 Answers 7

120

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");
Sign up to request clarification or add additional context in comments.

3 Comments

Considering big clauses with much information, do both cases have the same complexity or is the first one slower?
@alaxid: I would expect the second one to be slightly slower, because each Where call incurs a small extra overhead (method call, delegate invocation etc). I wouldn't be worried about this in any normal day-to-day scenarios though.
What about OR clause? Multiplying .Where would act as AND
8

no you can't define 2 delegates in the same where but you can build after each other or put both on same condition like this

 var result = codebase.Methods.Where(x => x.Body.Scopes.Count > 5 && x.Body.Scopes.name == "" );

or 

 var result = codebase.Methods.Where(x => x.Body.Scopes.Count > 5 )
        .where( y=> y.Body.Scopes.name == '' );

or 

 var result = codebase.Methods.Where(x => x.Body.Scopes.Count > 5  )
.Union( codebase.Methods.Where(y => y.Body.Scopes.name == ''  ) );

Comments

3

In your example, where does y come from? The Where method takes a lambda with a single input parameter, which represents a single instance of the sequence you're operating against.

You can, of course, have multiple conditions against x:

Where(x => x.Foo > 5 && x.Bar < 3)

Comments

1

What would "y" represent?

You can just use a standard && condition. No need for a "y":

codebase.Methods.Where(x => x.Body.Scopes.Count > 5 && x.Body.SomethingElse < 14);

Comments

1

I don't get it. What can you not do?

codebase.Methods.Where(x => x.Head.IsHairy && x.Body != null && x.Body.Scopes.Count > 5); 

1 Comment

The y that is mystifying you is just sort of a stand-in for the <YourType> object to be tested against in the filter. It may be called an "Anonymous Delegate" but I am not sure. In any case, it just provides a variable for you to use to access the properties of your type in the lambda expression. You could use any legal variable name.
0

like this..

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

Comments

0
codebase.Methods.Where(x => x.Body.Scopes.Count > 5).Where(x => x.Body.Scopes.TypeName == "Scopes").Where(x => x.Body.Scopes.Level == LEVEL_HIGH);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.