添加到字典中的字典

添加到字典中的字典

问题描述:

这是我的第一篇文章!添加到字典中的字典

我不是一个特别自信的程序员,但我到了那里。

我的问题是,我有一个

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

如果字典不包含当前键(串),我可以轻松地添加键值,并已被填充,像这样的另一个字典.. 。

testDictionary.Add(userAgentResult, allowDisallowDictionary); 

这工作正常,我的问题是当我试图增加内部字典如果userAgentResult键已经存在。

我希望做这样......

testDictionary[userAgentResult].Add(allowDisallowDictionary); 

但。新增方法要两个参数,即字符串键和列表值。于是我接着写这个代码...

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

这也适用,我的问题是,这个字典中加入了许多次,当内部字典已经包含了试图要添加的关键,它显然是错误的。所以当

在这种情况下,您需要做的不是将条目添加到内部字典中。您需要将该值添加到外部字典的键值对中。只是这一次的值恰好是另一个字典:)

testDictionary[userAgentResult] = allowDisallowDictionary;

+0

对不起,它已经采取了这么久,我回答,但我用这个建议,并已似乎正常工作! – 2012-02-13 12:04:09

+0

太棒了!如果您认为这是正确的答案,请将其标记为答案。 – 2012-02-13 16:54:36

你需要对内部字典做外部字典。首先检查该密钥是否已存在列表。如果不创建它。然后使用已存在或已创建的列表。

也许我不明白你的问题。首先确保存在像这样的词典:

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

然后你可以*的添加项目,像这样:通过具有一个字典,因此加入键

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

我可能会简化这个“模拟”一个分组。

string key = userAgentResult + allowDisallowKey; 

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

testDictionary[key] = list; 

你只需要管理一个字典。

+0

这个建议帮了我,谢谢 – 2015-03-26 14:57:04

+0

这一个简化了整个事情。干杯!! – Arjmand 2017-03-24 15:59:21

当使用嵌套词典我通常使用这种方法:

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