I'm trying to work out the syntax for using an anonymous function inside the lambda of a linq .Where() call.
I'm using the Where to filter certain items from a list.
I want each part of the filter logic inside the Where. The logic is only useful inside the filter so I don't want to have to define any functions outside.
Here's a simplified & generalised example:
var filtered = myEnumerable.Where(item =>
item.PropertyA == 1 ||
item.PropertyB == 2 ||
item =>
{
var heavyResult = GetStuff(item); // Some heavyweight processing
return heavyResult.IsSomethingTrue() && heavyResult.IsSomethingElseTrue();
});
So I want the third line in the Where() to be an anonymous function taking item and returning a boolean.
Also, the function being called after the checks on PropertyA and PropertyB is intended to limit having to call GetStuff() if either of those lightweight comparisons already evaluated as true.
I can't do it all inline because I need to evaluate two properties from heavyResult.
This seems like it should be simple, but I don't seem to be able to find the right syntax, either by experimenting or Googling.