Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all the keys from the dictionary if the value matches for more than one key

Tags:

c#

I have a dictionary

private readonly Dictionary<string, WebResponse> _myDictionary;

Lets assume I have 10 values in the dictionary currently. I can able to add some values into it also I can delete the values based on the key present in the dictionary similar to below.

Remove:

_myDictionary.Remove(Key); 

where key is a string variable.

Is that possible to delete more than one key at a time if the values matches for more than one key. I have keys like {KAAR1, KAAR2, KAAR3, ABCDEF}. Now I need to delete all the keys which contains "KAAR". Is that possible to do.

Kindly help.

like image 654
Priya Avatar asked Jan 28 '23 18:01

Priya


1 Answers

Try this:

_myDictionary
    .Where(x => x.Key.Contains("KAAR"))
    .ToList()
    .ForEach(kvp => _myDictionary.Remove(kvp.Key));
like image 135
Enigmativity Avatar answered May 12 '23 20:05

Enigmativity