SamKr
9/19/2017 - 10:10 AM

Dictionary RemoveAll

Removes multiple items from dictionary, extensionmethod

public static class DictionaryExtensions
{
    public static void RemoveAll<TKey, TValue>(this Dictionary<TKey, TValue> dic,
        Func<TValue, bool> predicate)
    {
        var keys = dic.Keys.Where(k => predicate(dic[k])).ToList();
        foreach (var key in keys)
        {
            dic.Remove(key);
        }
    }
}