John L.
John L.

Reputation: 1953

Checking if a non-list variable satisfies a predicate expression

If I have a predicate expression returning method called GetPredicate that I use for a list of users such as return u => u.age == 30, is there a better way to check if a single item user1 satisfies this condition than doing this:

var validUser = (new List<User>(){ user1}).Where(GetPredicate()).SingleOrDefault();

Because this feels kind of hacky.

Upvotes: 0

Views: 32

Answers (1)

adjan
adjan

Reputation: 13652

A predicate is just a delegate that can be invoked like a regular method:

var validUser = GetPredicate()(user1);

Upvotes: 1

Related Questions