I need to remove several items from the Dictionary. A simple way to do this:
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);
The reason I cannot directly delete elements in the foreach block is because it will throw an exception ("Collection has been changed ...")
I would like to do the following:
MyCollection.RemoveAll(x =>x.Member==foo)
But the Dictionary <> class does not provide the RemoveAll (Predicate <> Match) method, as does the List <> Class.
What is the best way (smart as well as elegant) to do this?
Brann Jan 22 '09 at 13:57 2009-01-22 13:57
source share