2012-02-08 19 views
5

これは私の最初の投稿です!辞書内の辞書に追加する

私は特に自信を持ったプログラマーではありませんが、そこに着いています。辞書は、現在のキー(文字列)が含まれていない場合は

私の問題は、私は

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

を持っていることを、私は簡単にキーなどのように、移植された別の辞書を追加することができます。.. 。正常に動作します

testDictionary.Add(userAgentResult, allowDisallowDictionary); 

は、私の問題は、私はuserAgentResultキーがすでに存在する場合は、内部辞書を追加しようとしていたときに付属しています。

私はこの方法でそれを行うには期待していた...

testDictionary[userAgentResult].Add(allowDisallowDictionary); 

はなく、.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); 

も動作します。これは、私の問題は、この辞書は、多数回に追加されていることを行って、内側の辞書はすでに追加されるようにしようとしているキーが含まれている場合、それは明らかに誤りです。だから

答えて

3

この場合、内部ディクショナリにエントリを追加しないでください。外部辞書のキーと値のペアに値を追加する必要があります。値がまだ別の辞書:)

testDictionary[userAgentResult] = allowDisallowDictionary;

+0

申し訳ありませんが、それは私が返事するのはそう長くかかったが、私はこの提案を使用し、うまく動作するように見えたこと! –

+0

素晴らしい!これが正しい答えだと感じる場合は、それを回答としてマークしてください。 –

0

あなたが外側のためにした内側の辞書に対して同じことをする必要があります。まず、このキーのリストがすでに存在するかどうかを確認します。それを作成しない場合。次に、存在したか作成されたリストを使用します。

1

であることを起こるのみ、この時間は、たぶん私はあなたの問題を得ることはありません。まず辞書はそうのように存在していることを確認してください:

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

私はおそらく「シミュレート」1つの辞書を持つため、キーを結合して、これを単純化しますグループ化。

string key = userAgentResult + allowDisallowKey; 

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

testDictionary[key] = list; 

1つの辞書を管理するだけで済みます。

+0

この提案は私を助けました、ありがとう –

+0

これは全体を単純化します。乾杯!! – Arjmand

1

私は通常、このアプローチを使用して、ネストされた辞書を使用して:

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