我需要从Dictionary中删除多个项目.一种简单的方法如下:
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);
Run Code Online (Sandbox Code Playgroud)
我无法直接删除foreach块中的项目的原因是这会抛出异常("Collection was modified ...")
我想做以下事情:
MyCollection.RemoveAll(x =>x.Member==foo)
Run Code Online (Sandbox Code Playgroud)
但是Dictionary <>类没有公开RemoveAll(Predicate <> Match)方法,就像List <> Class那样.
这样做的最佳方式(性能明智和优雅明智)是什么?