简体   繁体   English

C#中的词典词典

[英]Dictionary Of Dictionaries in C#

I am reading an SQL table from SQl SERVER using C# code. 我正在使用C#代码从SQl SERVER读取SQL表。

My data looks like this: 我的数据如下:

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

I wrote an sql reader command and it goes 我写了一个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();

             }
        }

I created a Dictionary as follows: 我创建了一个Dictionary如下:

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

I would ideally like to have a structure like this: 我希望有一个像这样的结构:

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

     EGG:
        {
        MN: 7,
        Tu: 0
        }


    }
    GOR:
        .
        .
        .
        .
        .

}

I am hoping to convert this dictionary to a json file but I am unable to add keys and values to the empty dictionary. 我希望将此字典转换为json文件,但我无法将键和值添加到空字典中。 I come from python background where i can use defaultdict to create dictionary of dictionaries without running to the key errors. 我来自python背景,我可以使用defaultdict创建字典字典而不会遇到键错误。

Any tips to create the dictionary will be hugely appreciated. 任何创建字典的提示都将非常受欢迎。


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

You can fill the dictionary directly in your loop 您可以直接在循环中填写字典

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

As a side note, you might want to consider creating classes to represent your objects, add them to a dictionary or list, and use Newtonsoft Json.net ( http://www.newtonsoft.com/json ) to serialize them instead of using dictionaries for everything. 作为旁注,您可能需要考虑创建表示对象的类,将它们添加到字典或列表中,并使用Newtonsoft Json.net( http://www.newtonsoft.com/json )序列化它们而不是使用它们一切都是字典。 For example, you can create a class: 例如,您可以创建一个类:

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 */ });

Of course, you can take this one step further and create a class with properties string Location and List<Animal> Animals , and eliminate dictionaries altogether. 当然,您可以更进一步,创建一个具有属性string LocationList<Animal> Animals ,并完全删除字典。 It depends on your use case(s) I guess 这取决于你的用例我想

public class DataLine
{
    public string Zoo { get; set; }
    public string Animal { get; set; }
    public int Mn { get; set; }
    public int Tu { get; set; }
}
public IEnumerable<DataLine> ReadAllLines()
{
    ...
    using (OleDbDataReader reader = cmd.ExecuteReader())
    {
        while (reader.Read())
        {
            yield return new DataLine() { Zoo = reader.GetString(0).Trim(), Animal = reader.GetString(1).Trim(), Mn = reader.GetInt32(2), Tu = reader.GetInt32(3) };
        }
    }
}
var dictionary = ReadAllLines()
    .GroupBy(line => line.Zoo)
    .ToDictionary(zoo => zoo.Key,
                  zoo => zoo.ToDictionary(animal => animal.Animal,
                                          animal => new { animal.Mn, animal.Tu }));

var sb = new StringBuilder();

using (var sw = new System.IO.StringWriter(sb))
{
    new Newtonsoft.Json.JsonSerializer().Serialize(sw, dictionary);
}

Console.WriteLine(sb.ToString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM