Goran
Goran

Reputation: 77

Passing multiple arguments to Predicate<T>

I am calling a filtering method that accepts parameter Predicate T

void MethodName(Predicate<T> param)
{
}

x.MethodName(x => x.SomeProperty == 10); // or
x.MethodName(delegate(MyClass x) {return x.SomeProperty == 10;});

The problem is that I need to be able to filter on several different values, and the number of values is unknow. Example with 3 values ouwld be

x.MethodName(x => x.SomeProperty == 10 || x => x.SomeProperty == 20); // or
x.MethodName(delegate(MyClass x) {return x.SomeProperty == 10 || x.SomeProperty == 20;});

Can I make it work with uknown number of possible values? This would be greate, but its not possilbe :)

x.MethodName(x => x.SomeProperty == {10, 20});

Upvotes: 2

Views: 3000

Answers (1)

Bala R
Bala R

Reputation: 108997

x.MethodName(x => new []{10, 20}.Contains(x.SomeProperty));

Upvotes: 2

Related Questions