c# - Using Linq to filter out certain Keys from a Dictionary and return a new dictionary -


i trying figure out linq query filter out list of keys dictionary , return new filtered out dictionary

var alldictenteries = new dictionary<string, string>                                      {                                          {"key1", "value1"},                                          {"key2", "value2"},                                          {"key3", "value3"},                                          {"key4", "value4"},                                          {"key5", "value5"},                                          {"key6", "value6"}                                      }; var keystobefiltered = new list<string> {"key1", "key3", "key6"}; 

the new dictionary should contain following entries

"key2", "value2" "key4", "value4" "key5", "value5" 

i don't want make copy of original dictionary , dictionary.remove, thinking there might , efficient way that.

thanks help

you can filter original dictionary, , use todictionary on result:

var keystobefiltered = new hashset<string> {"key1", "key3", "key6"}; var filter = alldictenteries     .where(p => !keystobefiltered.contains(p.key))     .todictionary(p => p.key, p => p.value); 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -