Question
Combine Multiple Predicates
Is there any way in c# .NET 2.0! to combine multiple Predicates?
Let's say I have the following code.
List<string> names = new List<string>();
names.Add("Jacob");
names.Add("Emma");
names.Add("Michael");
names.Add("Isabella");
names.Add("Ethan");
names.Add("Emily");
List<string> filteredNames = names.FindAll(StartsWithE);
static bool StartsWithE(string s)
{
if (s.StartsWith("E"))
{
return true;
}
else
{
return false;
}
}
This gives me:
Emma
Ethan
Emily
So this is pretty cool stuff, but I know want to be able to filter using multiple predicates.
So I want to be able to say something like this:
List<string> filteredNames = names.FindAll(StartsWithE OR StartsWithI);
In order to get:
Emma
Isabella
Ethan
Emily
How can I achieve this? Currently I am just filtering the complete list twice and combining the results afterwards. But unfortunately this is quite inefficent and even more importantly I lose the original sort order, which is not acceptable in my situation.
I also need to be able to iterate over any number of filters/predicates as there can be quite a lot.
Again it needs to be a .NET 2.0 solution unfortunately I can't use a newer version of the framework
Thanks a lot.