/// <summary>
/// 根据条件删除字典中所有符合的键值对
/// </summary>
/// <returns></returns>
public static int RemoveAll<TKey, TValue>(this IDictionary<TKey, TValue> source, Func<TKey, TValue, bool> predicate)
{
List<TKey> listTKey = new List<TKey>();
foreach (KeyValuePair<TKey, TValue> kv in source)
{
if (predicate(kv.Key, kv.Value))
{
listTKey.Add(kv.Key);
}
}
foreach (TKey k in listTKey)
{
source.Remove(k);
}
return listTKey.Count;
}
/// <summary>
/// 判断字典中是否存在符合条件的键值对
/// </summary>
public static bool Exists<TKey, TValue>(this IDictionary<TKey, TValue> source, Func<TKey, TValue, bool> predicate)
{
List<TKey> listTKey = new List<TKey>();
foreach (KeyValuePair<TKey, TValue> kv in source)
{
if (predicate(kv.Key, kv.Value))
{
return true;
}
}
return false;
}