Add to dictionary in dictionary

I'm not a very confident programmer yet, but I get there.

My problem is that I have

    static Dictionary<string, Dictionary<string, List<string>>> testDictionary = ...

If the dictionary does not contain the current key (string), I can easily add the key and another filled dictionary, for example ...

   testDictionary.Add(userAgentResult, allowDisallowDictionary);

This works fine, my problem occurs when I try to add an internal dictionary if the userAgentResult key already exists.

I was hoping to do it this way ...

    testDictionary[userAgentResult].Add(allowDisallowDictionary);

but the .Add method wants two arguments, that is, a row key and a list value. So, I continued to write this code ...

    //this list as the dictionary requires a list
    List<string> testDictionaryList = new List<string>();
    //this method returns a string
    testDictionaryList.Add(regexForm(allowResult, url));
    //this will add the key and value to the inner dictionary, the value, and then     
    //add this value at the userAgentKey
    testDictionary[userAgentResult].Add(allowDisallowKey, testDictionaryList);

This also works, my problem is that this dictionary is added many times, and when the internal dictionary already contains the key that it is trying to add, this is obviously an error. So when

+6
source share
5 answers

. - . :)

testDictionary[userAgentResult] = allowDisallowDictionary;

+3

, , , , , "" .

 string key = userAgentResult + allowDisallowKey;

 static Dictionary<string, List<string> testDictionary = ...

 testDictionary[key] = list;

.

+4

Perhaps I do not have your problem. First make sure dictionaries exist like this:

if (!testDictionary.ContainsKey(userAgentResult))
    testDictionary[userAgentResult] = new Dictionary<string, List<string>>();
if (!testDictionary[userAgentResult].ContainsKey(allowDisallowKey))
    testDictionary[userAgentResult][allowDisallowKey] = new List<string>();

Then you can add these elements:

testDictionary[userAgentResult][allowDisallowKey].Add("some value");
testDictionary[userAgentResult][allowDisallowKey].AddRange(someValueList);
+1
source

When using nested dictionaries, I usually use this approach:

private static Dictionary<string, Dictionary<string, List<string>>> _NestedDictionary = new Dictionary<string, Dictionary<string, List<string>>>();

private void DoSomething()
{
    var outerKey = "My outer key";
    var innerKey = "My inner key";
    Dictionary<string, List<string>> innerDictionary = null;
    List<string> listOfInnerDictionary = null;

    // Check if we already have a dictionary for this key.
    if (!_NestedDictionary.TryGetValue(outerKey, out innerDictionary))
    {
        // So we need to create one
        innerDictionary = new Dictionary<string, List<string>>();
        _NestedDictionary.Add(outerKey, innerDictionary);
    }

    // Check if the inner dict has the desired key
    if (!innerDictionary.TryGetValue(innerKey, out listOfInnerDictionary))
    {
        // So we need to create it
        listOfInnerDictionary = new List<string>();
        innerDictionary.Add(innerKey, listOfInnerDictionary);
    }

    // Do whatever you like to do with the list
    Console.WriteLine(innerKey + ":");
    foreach (var item in listOfInnerDictionary)
    {
        Console.WriteLine("   " + item);
    }
}
+1
source

You need to do the same for the internal dictionary that you did for the external. First check if a list exists for this key. If not create it. Then use the list that existed or was created.

0
source

All Articles