If you do not care about creating a new dictionary with the necessary items and discarding the old one, just try:
dic = dic.Where(i => i.Value.BooleanProperty) .ToDictionary(i => i.Key, i => i.Value);
If you cannot create a new dictionary and for some reason change the old one, for example, when it refers to external links, and you cannot update all the links:
foreach (var item in dic.Where(item => !item.Value.BooleanProperty).ToList()) dic.Remove(item.Key);
Please note that ToList needed here since you are ToList base collection. If you change the base collection, the enumerator working on it to query the values โโwill be unusable and will throw an exception in the next iteration of the loop. ToList caches values โโbefore changing the dictionary at all.
Mehrdad Afshari Jan 25 '10 at 10:44 2010-01-25 10:44
source share