C# 添加到字典中的字典

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9202683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:21:10  来源:igfitidea点击:

Adding to a Dictionary within a dictionary

c#.netdictionary

提问by Michael B

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.

效果很好,如果 userAgentResult Key 已经存在,我在尝试添加内部字典时会出现问题。

I was hoping to do it this way...

我希望以这种方式做到这一点......

    testDictionary[userAgentResult].Add(allowDisallowDictionary);

but the .Add method wants two arguments, i.e. the string key and list value. So I went on to write this code...

但是 .Add 方法需要两个参数,即字符串键和列表值。所以我继续写这段代码......

    //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

这也有效,我的问题是这个字典被添加了很多次,当内部字典已经包含试图添加的键时,它显然是错误的。所以当

采纳答案by Sofian Hnaide

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;

testDictionary[userAgentResult] = allowDisallowDictionary;

回答by usr

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.

您需要对内部字典执行与外部字典相同的操作。首先检查此键的列表是否已存在。如果没有创建它。然后使用存在或创建的列表。

回答by Nikola Radosavljevi?

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);

回答by Vince Panuccio

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.

您只需要管理一本字典。

回答by Oliver

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);
    }
}