How about:
public static Predicate<T> Or<T>(params Predicate<T>[] predicates)
{
return delegate (T item)
{
foreach (Predicate<T> predicate in predicates)
{
if (predicate(item))
{
return true;
}
}
return false;
};
}
And for completeness:
public static Predicate<T> And<T>(params Predicate<T>[] predicates)
{
return delegate (T item)
{
foreach (Predicate<T> predicate in predicates)
{
if (!predicate(item))
{
return false;
}
}
return true;
};
}
Then call it with:
List<string> filteredNames = names.FindAll(Helpers.Or(StartsWithE, StartsWithI));
Another alternative would be to use multicast delegates and then split them using GetInvocationList()
, then do the same thing. Then you could do:
List<string> filteredNames = names.FindAll(Helpers.Or(StartsWithE+StartsWithI));
I’m not a huge fan of the latter approach though – it feels like a bit of an abuse of multicasting.