Search code examples
c#dictionarycollectionsseries

c# Collection of Dictionary


how would I create and then access a collection of a (Sorted)Dictionary? It could be a Series<SortedDictionary<double, MyClass>> or a Dictionary<int, SortedDictionary<double, MyClass>> for example.

Series[0] = sortedDict;
Dict.Add(myInt, sortedDict);

For some reason, when doing it like this, the Dictionary is empty, when accessing values with a loop and the series seems to be empty as well.

for (int i = 0; i < Dict.Count; i++)
{
   SortedDictionary<double, MyClass> sortedDictPair = Dict.ElementAt(i).Value;
   foreach (var item in sortedDictPair)
   {
        Print(item.Key);
        MyClass myClass= item.Value;
        Print(myClass.classMember);
    }
}

SortedDictionary<double, MyClass> sortedDictPair = Series[0];
for (int j = 0; j < sortedDictPair.Count; j++)
    {
        MyClass myClass = sortedDictPair.ElementAt(j).Value;
        Print(myClass.classMember);
    }
 }

Is there something I am overseeing? Edit: when I let it print the sortedDict before aissgning it, it is full of entries.


Solution

  • Can you try them in two foreach loops?

    Dictionary<int, SortedDictionary<double, MyClass>> dictionary = new Dictionary<int, SortedDictionary<double, MyClass>>()
        {
            { 1, new SortedDictionary<double,MyClass>(){ {2, new MyClass{ Score=1 } }, { 3, new MyClass { Score = 1 } } } },
            { 2, new SortedDictionary<double,MyClass>()}
        };
        foreach (var item1 in dictionary)
        {
            foreach (var item2 in item1.Value)
            {
                Console.WriteLine(item2.Key);
                MyClass myClass = item2.Value;
                Console.WriteLine(myClass.Score);
            }
        }
    

    Check