【问题标题】:C# add a dictionary in a DictionaryC#在字典中添加字典
【发布时间】:2016-02-11 01:42:11
【问题描述】:

我正在尝试将数据存储在带有键的排序字典中,值是另一个字典。

我已经定义了一个类:

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 

我尝试了 Internet 上建议的几种不同方法,但要么出现错误,要么在添加“旧”键值时,它也覆盖了“今天”值。所以我已经为此苦苦挣扎了几天。

任何帮助将不胜感激。如果你能提供一个正确代码的例子,那将是有利的。

谢谢, 安德鲁

【问题讨论】:

  • 你把它叫做.Add([value])而不是.Add([key],[value])
  • Idk 如果只有我,但您的代码很难阅读 - 也许我只是累了。但是 Mathus 是正确的,你错过了你的 Key 定义。我认为您的 Dictionary 声明也有问题,但重新阅读了几次,我知道发生了什么。
  • 您使用的命名约定是一团糟。考虑遵循 C# 命名准则。
  • 如果我将其更改为 dLine.Add(sKey2, lsSearch_Config_Out_List);我仍然收到错误 - 参数 2:无法从 'System.Collections.Generic.List' 转换为 'System.Collections.Generic.List'
  • 对不起命名约定,但我一直在复制/粘贴/复制/粘贴互联网上的建议,试图遵循其他人的做法

标签: c# list dictionary


【解决方案1】:

除了 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

【讨论】:

  • 添加“Today”后,不会添加“Older”,因为主键 sKey 会重复。
  • 一切都好。我可以为重复的主键添加辅助字典 if (!dA.ContainsKey(sKey)) dA.Add(sKey, dicNew) else { dA[sKey].Add(sKey2, null); dA[sKey][sKey2] = (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));
【解决方案2】:

好的,这段代码非常难以阅读,所以这有点在黑暗中猜测。

第一个错误行:

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

【讨论】:

  • 如果我尝试 dLine[sKey2].AddRange(lsSearch_Config_Out_List);我收到错误参数 1:无法从 'System.Collections.Generic.List' 转换为 'System.Collections.Generic.IEnumerable'
猜你喜欢
  • 2023-03-20
  • 2018-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-22
  • 2021-09-12
相关资源
最近更新 更多