Best way to remove multiple items matching a predicate from a c# Dictionary?

前端 未结 7 470
后悔当初
后悔当初 2020-12-09 01:17

I need to remove multiple items from a Dictionary. A simple way to do that is as follows :

  List keystoremove= new List();
  for         


        
7条回答
  •  伪装坚强ぢ
    2020-12-09 01:59

    Instead of removing just do the inverse (create a new dictionary from the old one containing only the elements you are interested in) and let the garbage collector take care of the old dictionary:

    var newDictionary = oldDictionary.Where(x => x.Value != foo);
    

提交回复
热议问题