【发布时间】:2010-10-02 22:08:58
【问题描述】:
我需要从字典中删除多个项目。 一个简单的方法如下:
List<string> keystoremove= new List<string>();
foreach (KeyValuePair<string,object> k in MyCollection)
if (k.Value.Member==foo)
keystoremove.Add(k.Key);
foreach (string s in keystoremove)
MyCollection.Remove(s);
我不能直接删除foreach块中的项目的原因是这样会抛出异常(“Collection was modified...”)
我想做以下事情:
MyCollection.RemoveAll(x =>x.Member==foo)
但是 Dictionary 类不像 List 类那样公开 RemoveAll(Predicate Match) 方法。
最好的方法是什么(性能方面和优雅方面)?
【问题讨论】:
标签: c# .net linq collections dictionary