我想用一个键将数据存储在一个已排序字典中,并且该值是另一个字典。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#命名事物的指导原则。 –