I have a C# dictionary, Dictionary that I need to be filtered based on a property of MyObject.
For example, I want to
I added the following extension method for my project which allows you to filter an IDictionary.
public static IDictionary KeepWhen(
this IDictionary dict,
Predicate predicate
) {
return dict.Aggregate(
new Dictionary(),
(result, keyValPair) =>
{
var key = keyValPair.Key;
var val = keyValPair.Value;
if (predicate(val))
result.Add(key, val);
return result;
}
);
}
Usage:
IDictionary oldPeople = personIdToPerson.KeepWhen(p => p.age > 29);