2016-02-11 104 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