I have a C# dictionary, Dictionary
that I need to be filtered based on a property of MyObject
.
For example, I want to
You can simply use the Linq where clause:
var filtered = from kvp in myDictionary
where !kvp.Value.BooleanProperty
select kvp
I added the following extension method for my project which allows you to filter an IDictionary.
public static IDictionary<keyType, valType> KeepWhen<keyType, valType>(
this IDictionary<keyType, valType> dict,
Predicate<valType> predicate
) {
return dict.Aggregate(
new Dictionary<keyType, valType>(),
(result, keyValPair) =>
{
var key = keyValPair.Key;
var val = keyValPair.Value;
if (predicate(val))
result.Add(key, val);
return result;
}
);
}
Usage:
IDictionary<int, Person> oldPeople = personIdToPerson.KeepWhen(p => p.age > 29);
public static Dictionary<TKey, TValue> Where<TKey, TValue>(this Dictionary<TKey, TValue> instance, Func<KeyValuePair<TKey, TValue>, bool> predicate)
{
return Enumerable.Where(instance, predicate)
.ToDictionary(item => item.Key, item => item.Value);
}
Since Dictionary implements IEnumerable<KeyValuePair<Key, Value>>
, you can just use Where:
var matches = dictionary.Where(kvp => !kvp.Value.BooleanProperty);
To recreate a new dictionary if you need it, use the ToDictionary method.
If you don't care about creating a new dictionary with the desired items and throwing away the old one, simply try:
dic = dic.Where(i => i.Value.BooleanProperty)
.ToDictionary(i => i.Key, i => i.Value);
If you can't create a new dictionary and need to alter the old one for some reason (like when it's externally referenced and you can't update all the references:
foreach (var item in dic.Where(item => !item.Value.BooleanProperty).ToList())
dic.Remove(item.Key);
Note that ToList
is necessary here since you're modifying the underlying collection. If you change the underlying collection, the enumerator working on it to query the values will be unusable and will throw an exception in the next loop iteration. ToList
caches the values before altering the dictionary at all.
Here is a general solution, working not only for boolean properties of the values.
Method
Reminder: Extension methods must be placed in static classes. Dont forget the using System.Linq;
statement at the top of the source file.
/// <summary>
/// Creates a filtered copy of this dictionary, using the given predicate.
/// </summary>
public static Dictionary<K, V> Filter<K, V>(this Dictionary<K, V> dict,
Predicate<KeyValuePair<K, V>> pred) {
return dict.Where(it => pred(it)).ToDictionary(it => it.Key, it => it.Value);
}
Usage
Example:
var onlyWithPositiveValues = allNumbers.Filter(it => it.Value > 0);