我想用一個鍵將數據存儲在一個已排序字典中,並且該值是另一個字典。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
我已經嘗試了幾種不同的方式在互聯網上建議,但我要麼得到一個錯誤或添加時'老'鍵值,它也覆蓋'今日'值。所以我一直在爲此奮鬥了幾天。
任何幫助將不勝感激。如果你能提供一個有利的正確代碼的例子。
感謝, 安德魯
你說它像'。新增([值])''來代替。添加([key],[value])# –
Idk如果只是我,但你的代碼很難閱讀 - 也許我只是很累。但Mathus是正確的,你錯過了'Key'定義。我認爲你的'Dictionary'聲明有問題,但是有一些重新閱讀,我知道發生了什麼。 – Gabe
你使用的命名約定是一團糟。考慮遵循C#命名事物的指導原則。 –