簡體   English   中英

C#在字典中添加字典

[英]C# add a dictionary in a Dictionary

我試圖將數據存儲在帶有鍵的排序字典中,而值是另一個字典。

我已經定義了一個類:

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文件。 可以有兩種類型:1表示'今天',1表示'較舊'表示相同的鍵。

 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                                       

最后我希望Dictionary存儲這樣的東西:

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 

我在互聯網上嘗試了幾種不同的方式,但我要么出錯,要么在添加'舊'鍵值時,它也會覆蓋'今日'值。 所以我幾天都在努力解決這個問題。

任何幫助將非常感激。 如果您能提供一個有利的正確代碼示例。

謝謝,安德魯

還有更多只是List to Dictionary

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

好的,這段代碼很難閱讀,所以這在黑暗中有點猜測。

第一個錯誤行:

dLine[sKey2].Add(lsSearch_Config_Out_List); "****ERROR has invalid arguments

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

第二個錯誤行:

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

我完全不知道你在這里想要完成什么(或者例如diSearch_Config_List來自哪里),但是你試圖在不提供密鑰的情況下將List添加到Dictionary

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM