Hızlı yanıt: kod örneği
You can fill the dictionary directly in your loopAs 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. For example, you can create a class:Of course, you can take this one step further and create a class with properties and , and eliminate dictionaries altogether. It depends on your use case(s) I guess
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 } };}
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 thisif(!zooDict.ContainsKey(location)) zooDict[location] = new List<Animal>();zooDict[location].Add(new Animal() { /* fill Monday and Tuesday properties */ });
string Location
List<Animal> Animals