Click to See Complete Forum and Search --> : Dictionary<string, string> remove 2 or more items


asifakhtar
03-01-2009, 08:13 PM
How can I remove 2 or more items from a Dictionary<string, string>. With my code I am getting the following error:
“Collection was modified; enumeration operation may not execute.”
Here is my coding:
foreach (KeyValuePair<string, string> pair in tmpList)
{

if (pair.Value == "a" || pair.Value == "b")
{
tmpList.Remove(pair.Key);

}
}

lmf232s
03-04-2009, 04:41 PM
Not sure which version of .net your using but if your on 3 or 3.5 then you can use this example which uses LINQ and will avoid the enumeration issue your having:
http://stackoverflow.com/questions/469202/best-way-to-remove-multiple-items-matching-a-predicate-from-a-c-dictionary