I have a dictionary, where the key is a string and the value is a list of strings that correspond to that key. I would like to display all of the keys in the dictionary, with the values associated with that key tabbed in underneath that key. Something like this:
Key 1
Value 1
Value 2
Value 3
Key 2
Value 1
Value 2
In C# 2.0, I would do that like this (values
is the Dictionary
):
StringBuilder sb = new StringBuilder();
foreach(KeyValuePair<string, List<string>> pair in values)
{
sb.AppendLine(pair.Key);
foreach(string item in pair.Value)
{
sb.AppendLine('\t' + item);
}
}
How would I do the equivalent using LINQ? It seems like it should be possible, however I can't figure out how to do it.
If I use values.SelectMany(p => p.Values)
, then only the values will be in the final result, not the keys as well.
Any other solution that I've thought of has a similar limitation.