0

I was wondering if there is an easy way to place two Lambda expressions in a single (Linq/Where) query?

For example, I currently call a method with something like the following:

string testing = "blablabla"; if(testing == "" || testing == null) 

I have tried a few combinations such as:

testing.Where(x => x == ("") || x=> x == null); 

But the above doesn't work. I know I can set up a method that returns a predicate/bool, but, at the moment, I am interested in Lambdas and was just wondering how to achieve this.

Do I need to chain multiple Where methods, or is there a way to achieve multiple Lambdas?

(p.s. I know about IsNullOrEmpty, this is just the first example I could think of!)

2
  • Does String.IsNullOrEmpty not work? Commented Sep 25, 2011 at 2:12
  • @Brad Christie and others - I know about IsNullOrEmpty - I have a slightly complicated method that would make no sense out of context and this was just the first example I could think of. Commented Sep 25, 2011 at 2:16

2 Answers 2

7

You can always combine them to a single lambda.

testing.Where(x => x == null || x == ("") ); 
Sign up to request clarification or add additional context in comments.

4 Comments

Brilliant - Thanks... Unless someone else gives a better answer/huge explanation... I will mark this as answer when I can.
Yep, it makes the most sense to define a condition which each x must satisfy. Two separate lambda expressions is less logical
@Bala R - I am still learning about Lambdas... I certainly prefer this syntax to a whole separate method, but, do you happen to know speed / does it run quicker/slower than a separate method? ...
@WilliamHilsum I don't think there will be any difference in speed. Lambda expressions are function delegates and are syntactic sugar for convenience.
0

If you're looking for a general way to combine query conditions in arbitrary ways, you can use expression trees:

http://msdn.microsoft.com/en-us/library/bb882637.aspx

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.