Nano HE Nano HE - 2 months ago 8
C# Question

Complex Dictionary Print

I created a complex dictionary.

mainDict<mainKey, SubDict>
subDict<subKey, sub1Dict>
sub1Dict<sub1Key, sub2Dict>
sub2Dict<sub2Key, sub2Value>
.......


How to print it out like this (vs2005 & .net 2.0 based)

mainKey/subKey/sub1Key/sub2Key, sub2Value


Do i need convert mainDict to a List and call join()?

thank you.

Answer
StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<mainKey, SubDict> pair1 in mainDict)
foreach (KeyValuePair<subKey, sub1Dict> pair2 in pair1.Value)
foreach (KeyValuePair<sub1Key, sub2Dict> pair3 in pair2.Value)
foreach (KeyValuePair<sub2Key, sub2Value> pair4 in pair3.Value)
{
    sb.AppendFormat("{0}/{1}/{2}/{3}, {4}", 
        pair1.Key, pair2.Key, pair3.Key, pair4.Key, pair4.Value);
}

UPDATE:

StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<string, object> pair1 in mainDict)
foreach (KeyValuePair<string, object> pair2 in (Dictionary<string, object>)pair1.Value)
foreach (KeyValuePair<string, object> pair3 in (Dictionary<string, object>)pair2.Value)
foreach (KeyValuePair<string, object> pair4 in (Dictionary<string, object>)pair3.Value)
{
    sb.AppendFormat("{0}/{1}/{2}/{3}, {4}", 
        pair1.Key, pair2.Key, pair3.Key, pair4.Key, pair4.Value);
}
Comments