0

I have a:

Dictionary<string, Dictionary<int, List<string>>> res = new Dictionary<string, 
Dictionary<int, List<string>>>();

and I need to modify/change the int value of the nested Dictionary Key and keep all Dictionary values( List ) for the int Key.

2 Answers2

0

If I understood everything correctly:

res[stringKey].Add(newKey, res[oldKey]);
res[stringKey].Remove(oldKey);
0

There is no native way to achieve this that I know of but you can try the following:

private void ModifyKey(int oldKey, int newKey, Dictionay<int, List<string>> dict)
{
    var data = dict[oldKey];
    // Now remove the previous data
    dict.Remove(key);
    try
    {
        dict.Add(newKey, data);
    }
    catch
    {
        // one already exists..., perhaps roll back or throw
    }
}

You would then call the method as follows when you want to change the key:

// Assuming the dictionary is called myData
ModifyKey(5, 7, myData);
Fabulous
  • 2,393
  • 2
  • 20
  • 27