Reputation: 3
I have this example of dictionary:
private Dictionary<string, Dictionary<int, List<Configs>>> dict_of_all_configs
= new Dictionary<string, Dictionary<int, List<Configs>>>();
Now i want delete some item from the second dictionary, i know the 1ºKey(parentNode), 2ºKey(childNode):
public void DeleteSelectedConfig(bool parentNodeSelect, string parentNode, string childNode)
{
if ( parentNodeSelect )
dict_of_all_configs.Remove(parentNode);
else
{
//Dont work it:
dict_of_all_configs.Remove(dict_of_all_configs[parentNode][int.Parse(childNode)][0].ToString());
//dict_of_all_configs.Where(pair => pair.Key == parentNode).Select(pair =>
//{
// dict_of_all_configs.Remove(pair.Key);
// return pair.Key;
//});
}
Any idea? Thanks.
Upvotes: 0
Views: 63
Reputation: 270995
If I understand correctly, you want to remove a key value pair from the nested dictionary, given the key of the nested dictionary (parentNode
) and the key you want removed (childNode
).
You can do it like this (assuming the keys exist):
dict_of_all_configs[parentNode].Remove(int.Parse(childNode));
dict_of_all_configs[parentNode]
gives you the nested dictionary, which is the dictionary from which you want to .Remove(...)
a key. You should not call .Remove
on dict_of_all_configs
directly, because you don't want any keys to be removed from the outer dictionary.
Upvotes: 1