gpt4 book ai didi

C#在Dictionary中添加字典

转载 作者:太空宇宙 更新时间:2023-11-03 23:21:23 27 4
gpt4 key购买 nike

我正在尝试将数据存储在一个已排序的字典中,其中一个键和另一个字典的值为值。

我定义了一个类:

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 种用于“今天”,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

最后我想让字典存储这样的东西:

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

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

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

谢谢,安德鲁

最佳答案

除了从列表到字典还有更多内容

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; }
}

这不是您的代码,但您明白了,创建一个新对象然后将 IT 添加到列表中,或者使用内联实例化。

有了这段代码就到了 http://imgur.com/JIId3wT

关于C#在Dictionary中添加字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35329340/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com