Peter
Peter

Reputation: 41

How can I use a Lambda bool method within a WHERE clause?

I have a method IsMatchingRegex which will return true or false. I want to check whether the Lambda property matches against the IsMatchingRegEx. If it does match, it should be added to the validItems List. How can I make the Lambda Expression work without changing the TRUE/FALSE method?

validItems = items.Where(x => x.Sub.PropertyToCheck == IsMatchingRegex(x.Sub.PropertyToCheck))

Upvotes: 3

Views: 468

Answers (1)

Gilad Green
Gilad Green

Reputation: 37299

Why compare equality to the property? Just:

validItems = items.Where(x => IsMatchingRegex(x.Sub.PropertyToCheck));

The Where expects a predicate that given an item of the collection returns for it true or false. If you method already does that - just call it.

Upvotes: 2

Related Questions