I would like to know the most optimal/elegant solution of doing this. Basically i have 3 groups of data.
Each of the groups are inside another in a hierarchy. I’m using a dictionary within a dictionary to achieve this. Sample Code:
Dictionary<string,Dictionary<string,List<string>>> pro = new Dictionary<string,Dictionary<string,List<string>>>();
Dictionary<string,List<string>> part = new Dictionary<string,List<string>>();
List<string> foo = new List<string>();
List<string> bar = new List<string>();
foo.Add("foo1");
foo.Add("foo2");
foo.Add("foo3");
bar.Add("bar1");
bar.Add("bar2");
bar.Add("bar3");
part.Add("Part1", foo);
part.Add("Part2", bar);
pro.Add("First", part);
foreach (var pros in pro)
{
foreach (var parts in pros.Value)
{
foreach (var foos in parts.Value)
{
Console.WriteLine(foos);
}
}
}
I’m still new to c#, even though my code is working i still feel that there is a more elegant solution. Thanks in advance!
you could use collection initializers for the construction part and LINQ for the print part.
EDIT: provided new link for LINQ referring to another useful related post on stackoverflow.