2016-02-11 103 views
1

我想用一個鍵將數據存儲在一個已排序字典中,並且該值是另一個字典。C#在字典中添加字典

我已經定義了一個類:

public class Search_Config_Out_List 
{ 
    public string _Env { get; set; } 
    public string _Interface { get; set; } 
    public string _System { get; set; } 
    public string _Timeframe { get; set; } 
    public string _Dir { get; set; } 
    public string _Filename { get; set; } 
    public string _LogDir { get; set; } 
    public string _LogFile { get; set; } 
    public Search_Config_Out_List(string Env, string Interface, string System, string Timeframe, string Dir, string Filename, string LogDir, string LogFile) 
    { 
     _Env = Env; 
     _System = System; 
     _Interface = Interface; 
     _Timeframe = Timeframe; 
     _Dir = Dir; 
     _Filename = Filename; 
     _LogDir = LogDir; 
     _LogFile = LogFile; 
    } 
} 

定義:

SortedDictionary<string, Dictionary<string, Search_Config_Out_List>> dA = 
     new SortedDictionary<string, Dictionary<string, Search_Config_Out_List>>(); 
List<Search_Config_Out_List> lsSearch_Config_Out_List = new List<Search_Config_Out_List>(); 

在這一點上我已經通過一個XML文件中檢索數據的循環。可以有2種類型:1個用於'Today',1個用於'Older'用於同一個鍵。

lsSearch_Config_Out_List.Clear(); 
lsSearch_Config_Out_List.Add(
     new Search_Config_Out_List(nodeALL.Attributes["Name"].Value.ToString(), 
            childENV.Attributes["Name"].Value.ToString(), 
            intALL.Attributes["Name"].Value.ToString(), 
            intDTL.Attributes["Name"].Value.ToString(), 
            sDir, 
            sFilename, 
            sLogDir, 
            sLogFile)); 

//sKey is the key for dA 
string sKey = nodeALL.Attributes["Name"].Value.ToString() 
       + "-" + childENV.Attributes["Name"].Value.ToString() 
       + "-" + intALL.Attributes["Name"].Value.ToString(); 

//sKey2 is the key for the inside Dictionary (possible values are 'Today' and 'Older' 
string sKey2 = intDTL.Attributes["Name"].Value.ToString(); 
dLine = new Dictionary<string, List<string>>(); 
if (!dLine.ContainsKey(sKey2)) 
    dLine.Add(sKey2, new List<string>()); 
//Store in Dictionary 
dLine[sKey2].Add(lsSearch_Config_Out_List); "****ERROR has invalid arguments 

dA = new SortedDictionary<string, Dictionary<string, Search_Config_Out_List>>(); 
if (!dA.ContainsKey(sKey)) 
    dA.Add(sKey, new Dictionary<string, Search_Config_Out_List>()); 
dA[sKey].Add(dLine);  "**ERROR has invalid arguments          

在我想要的字典存儲這樣的結尾:

KEY="TEST-1" 
    VALUE[0] KEY="TODAY" 
         VALUE[0] Line1 
         VALUE[1] Line2 
         VALUE[2] Line3 
    VALUE[1] KEY="OLDER" 
         VALUE[0] Line1 
         VALUE[1] Line2 
KEY="TEST-2" 
    VALUE[0] KEY="TODAY" 
         VALUE[0] Line1 
    VALUE[1] KEY="OLDER" 
         VALUE[0] Line1 

我已經嘗試了幾種不同的方式在互聯網上建議,但我要麼得到一個錯誤或添加時'老'鍵值,它也覆蓋'今日'值。所以我一直在爲此奮鬥了幾天。

任何幫助將不勝感激。如果你能提供一個有利的正確代碼的例子。

感謝, 安德魯

+0

你說它像'。新增([值])''來代替。添加([key],[value])# –

+0

Idk如果只是我,但你的代碼很難閱讀 - 也許我只是很累。但Mathus是正確的,你錯過了'Key'定義。我認爲你的'Dictionary'聲明有問題,但是有一些重新閱讀,我知道發生了什麼。 – Gabe

+0

你使用的命名約定是一團糟。考慮遵循C#命名事物的指導原則。 –

回答

2

有更多的則只是列出來字典

dA.Add(sKey, new Dictionary<string, Search_Config_Out_List>()); 

要添加新的字典,但你需要一個實例,以不添加初始化。

 SortedDictionary<string, Dictionary<string, myClass>> dA = new SortedDictionary<string, Dictionary<string, myClass>>(); 
     Dictionary<string, myClass> lsSearchConfigOutList = new Dictionary<string, myClass>(); 



     lsSearchConfigOutList.Clear(); 
     lsSearchConfigOutList.Add("1", new myClass(name:"John", face:"Happy")); 
     lsSearchConfigOutList.Add("2", new myClass(name: "Frank", face: "Sad")); 
     //sKey is the key for dA 
     string sKey = "Test-1"; 
     //sKey2 is the key for the inside Dictionary (possible values are 'Today' and 'Older' 
     string sKey2 = "Today"; 

     Dictionary<string, Dictionary<string, myClass>> dLine = new Dictionary<string, Dictionary<string, myClass>> {{sKey2, lsSearchConfigOutList}}; 
     var dicNew = new Dictionary<string, myClass>() { {sKey2, new myClass("Tom", "Hero")} }; 

      dA.Add("key2", dicNew); 

其他類

class myClass 
{ 
    public myClass(string name, string face) 
    { 
     Name = name; 
     Face = face; 
    } 
    public string Name { get; set; } 
    public string Face { get; set; } 
} 

這不是你的代碼,但你的想法,創建一個新的對象,然後將其添加到列表或使用內聯實例。

有了這個代碼涉及到 http://imgur.com/JIId3wT

+0

謝謝,我會給它一個去 – AndrewH

+0

'今日'後被添加,'老'不會被添加,因爲主鍵sKey將是重複的。 – AndrewH

+0

一切都好。我可以添加輔助字典重複主鍵如果(!dA.ContainsKey(sKey))dA.Add(sKey,dicNew)else {dA [sKey] .Add(sKey2,null); DA [SKEY] [sKey2] =(新Search_Config_Out_List(nodeALL.Attributes [ 「名稱」]。Value.ToString(),childENV.Attributes [ 「名稱」]。Value.ToString(),intALL.Attributes [ 「名稱」] .Value.ToString(),intDTL.Attributes [「Name」]。Value.ToString(),sDir,sFilename,sLogDir,sLogFile)); – AndrewH

0

好這個代碼是認真刻苦讀書,所以這是一個有點黑暗猜測。

第一個錯誤行:

​​

這將嘗試列表添加到列表中。這不起作用。使用=運算符覆蓋字典中的值或使用dLine[sKey2].AddRange(...)附加到現有列表。

二錯誤行:

dA[sKey].Add(dLine); "**ERROR has invalid arguments

我絕對不知道你想什麼,在這裏完成(或其中,例如diSearch_Config_List來自),但你嘗試將List添加到Dictionary沒有提供鑰匙。

+0

,如果我嘗試鼎聯[sKey2] .AddRange(lsSearch_Config_Out_List)複製/粘貼;我得到錯誤參數1:無法從'System.Collections.Generic.List '轉換爲'System.Collections.Generic.IEnumerable ' – AndrewH