gpt4 book ai didi

c# - C# 中的字典词典

转载 作者:行者123 更新时间:2023-11-30 13:25:45 25 4
gpt4 key购买 nike

我正在使用 C# 代码从 SQl SERVER 读取 SQL 表。

我的数据是这样的:

ZOO   Animal    Mn Tu
NDS BAT 2 0
GOR BAT 3 1
VTZ BAT 1 2
MAS BAT 0 0
CST BAT 0 0
NDS CAT 4 0
GOR CAT 4 0
VTZ CAT 2 0
MAS CAT 7 0
CST CAT 1 0
NDS DOG 3 0
GOR DOG 2 0
VTZ DOG 1 0
MAS DOG 3 0
CST DOG 0 1
NDS EGG 7 0
GOR EGG 2 0
VTZ EGG 0 0
MAS EGG 0 0
CST EGG 4 0

我写了一个 sql reader 命令,它运行了

sql = "SELECT * FROM table";

OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.CommandTimeout = 600;
using (OleDbDataReader reader = cmd.ExecuteReader())
{


while (reader.Read())
{
var location = reader.GetString(0).Trim();
var animal= reader.GetString(1).Trim();
var Monday= reader.GetValue(1).ToString().Trim();
var Tuesday= reader.GetValue(3).ToString().Trim();

}
}

我创建了一个字典如下:

 private Dictionary<string, Dictionary<string, Dictionary<string, string>>> zooDict;
zooDict= new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();

理想情况下,我希望有这样的结构:

{
NDS:
{
BAT:
{Mn: 2,
Tu: 2},
CAT:
{
MN: 4,
Tu: 0
}
DOG:
{
MN: 3,
Tu: 0
}

EGG:
{
MN: 7,
Tu: 0
}


}
GOR:
.
.
.
.
.

}

我希望将此字典转换为 json 文件,但我无法将键和值添加到空字典中。我来自 python 背景,在那里我可以使用 defaultdict 创建字典字典而不会遇到关键错误。

任何创建字典的提示都将不胜感激。


if (!ZooDict.ContainsKey(location))
{
var catDict = new Dictionary<string, string>();
catDict.Add("Mon", Monday);
catDict.Add("Tue", Tuesday);
var AnimalDict = new Dictionary<string, Dictionary<string, string>>();
AnimalDict.Add(animal, catDict);
waitlistDict.Add(location, AnimalDict);
}
else
{
if (!waitlistDict[location].ContainsKey(animal))
{
var catDict = new Dictionary<string, string>();
catDict.Add("Mon", Monday);
catDict.Add("Tue", Tuesday);
var AnimalDict = new Dictionary<string, Dictionary<string, string>>();
AnimalDict.Add(animal, catDict);
waitlistDict[location] = AnimalDict;
}
else
{
if (waitlistDict[location][animal].ContainsKey("Mon"))
{
waitlistDict[location][animal]["Mon"] = Monday;
}
else
{
waitlistDict[location][animal]["Mon"] = Monday;
}
if (waitlistDict[location][animal].ContainsKey("Tue"))
{
waitlistDict[location][animal]["Tue"] = Tuesday;
}
else
{
waitlistDict[location][animal]["Tue"] = Tuesday;
}
}
}

最佳答案

你可以直接在你的循环中填充字典

private Dictionary<string, Dictionary<string, Dictionary<string, string>>> zooDict =
new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();

while (reader.Read())
{
var location = reader.GetString(0).Trim();
var animal= reader.GetString(1).Trim();
var Monday= reader.GetValue(1).ToString().Trim();
var Tuesday= reader.GetValue(3).ToString().Trim();

if(!zooDict.ContainsKey(location))
zooDict[location] = new Dictionary<string, Dictionary<string, string>>();

zooDict[location][animal] = new Dictionary<string, string>()
{
{ "MN", Monday },
{ "Tu", Tuesday }
};
}

作为旁注,您可能需要考虑创建类来表示您的对象,将它们添加到字典或列表中,然后使用 Newtonsoft Json.net ( http://www.newtonsoft.com/json ) 对它们进行序列化,而不是对所有内容都使用字典。例如,您可以创建一个类:

public class Animal
{
public string Name { get; set; }
public int Monday { get; set; }
public int Tuesday { get; set; }
}

var zooDict = new Dictionary<string, List<Animal>>();

// then in your while loop you do this
if(!zooDict.ContainsKey(location))
zooDict[location] = new List<Animal>();
zooDict[location].Add(new Animal() { /* fill Monday and Tuesday properties */ });

当然,您可以更进一步,创建一个具有属性 string Location 的类和 List<Animal> Animals , 并完全消除字典。我猜这取决于您的用例

关于c# - C# 中的字典词典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41029249/

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