Dictionary Of Dictionaries in C#
<p>I am reading an SQL table from SQl SERVER using C# code.</p>

<p>My data looks like this:</p>

<pre><code>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
</code></pre>

<p>I wrote an sql reader command and it goes </p>

<pre><code>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();

             }
        }
</code></pre>

<p>I created a Dictionary as follows:</p>

<pre><code> private Dictionary&lt;string, Dictionary&lt;string, Dictionary&lt;string, string&gt;&gt;&gt; zooDict;
 zooDict= new Dictionary&lt;string, Dictionary&lt;string, Dictionary&lt;string, string&gt;&gt;&gt;();
</code></pre>

<p>I would ideally like to have a structure like this:</p>

<pre><code>{
NDS:
    {
     BAT:
        {Mn: 2,
         Tu: 2},
     CAT:
        {
        MN: 4,
        Tu: 0
        }
     DOG:
        {
        MN: 3,
        Tu: 0
        }

     EGG:
        {
        MN: 7,
        Tu: 0
        }


    }
    GOR:
        .
        .
        .
        .
        .

}
</code></pre>

<p>I am hoping to convert this dictionary to a json file but I am unable to add keys and values to the empty dictionary. I come from python background where i can use defaultdict to create dictionary of dictionaries without running to the key errors. </p>

<p>Any tips to create the dictionary will be hugely appreciated. </p>

<hr>

<pre><code>if (!ZooDict.ContainsKey(location))
{
    var catDict = new Dictionary&lt;string, string&gt;();
    catDict.Add("Mon", Monday);
    catDict.Add("Tue", Tuesday);
    var AnimalDict = new Dictionary&lt;string, Dictionary&lt;string, string&gt;&gt;();
    AnimalDict.Add(animal, catDict);
    waitlistDict.Add(location, AnimalDict);
}
else
{
    if (!waitlistDict[location].ContainsKey(animal))
    {
        var catDict = new Dictionary&lt;string, string&gt;();
        catDict.Add("Mon", Monday);
        catDict.Add("Tue", Tuesday);
        var AnimalDict = new Dictionary&lt;string, Dictionary&lt;string, string&gt;&gt;();
        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;
        }
    }
}
</code></pre>