简体   繁体   中英

Adding to a Dictionary within a dictionary

I am not a particularly confident programmer yet but am getting there.

My problem is that I have a

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

If the Dictionary doesn't contain the current key (string), I can easily add the key and another dictionary that has been populated, like so...

   testDictionary.Add(userAgentResult, allowDisallowDictionary);

That works fine, my problem comes when I am trying to add the inner 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, ie the string key and list value. So I went on 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 to numerous times, and when the inner dictionary already contains the key that is trying to be added, it obviously errors. So when

I would probably simplify this by having one dictionary and joining the keys thus "simulating" a grouping.

 string key = userAgentResult + allowDisallowKey;

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

 testDictionary[key] = list;

You simply need to manage one dictionary.

In this case what you need to do is not adding an entry to the inner dictionary. You need to add the value to the key-value pair of the outer dictionary. Only this time the value happens to be yet another dictionary :)

testDictionary[userAgentResult] = allowDisallowDictionary;

Maybe i don't get your problem. First make sure that dictionaries exist like so:

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

Then you are free to add items like so:

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

When using nested dictionaries i normally 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);
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM