tags:

views:

108

answers:

2

Hi

how to use "And" in predicate in lambda expression. I am trying to achieve something like this
new Class1("Test",Status => Status == 18 && 19 && 20)

Please reply

Thanks Sharath

+8  A: 

You need to break up your conditional statement into 3 different evaluations - you cannot combine them like you have.

Status => Status == 18 && Status == 19 && Status == 20

Although, I'm guessing you're wanting an or operator here, because Status cannot have a value of 18, 19, AND 20. If you're trying to do a bitmask, you want the & operator, and even then a bitmask of 18, 19 & 20 doesn't make a lot of sense to me (though who knows your use case).

Status => Status == 18 || Status == 19 || Status == 20
Matt
Thanks a lot it worked for me.
@Sharath this is when you click 'Accept' on @Matt's answer
jeroenh
A: 

Good answer so far - consider also .Any

List<int> favNumbers = new List<int>(18, 19, 20);
Func<int, bool> pred =
  Status => favNumbers.Any(f => f == Status) ;
David B