Best way to remove multiple items matching a predicate from a .NET Dictionary?

Here’s an alternate way

foreach ( var s in MyCollection.Where(kv => kv.Value.Member == foo).ToList() ) {
  MyCollection.Remove(s.Key);
}

Pushing the code into a list directly allows you to avoid the “removing while enumerating” problem. The .ToList() will force the enumeration before the foreach really starts.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)