Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 185189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:25:35+00:00 2026-05-11T15:25:35+00:00

I have a dictionary, where the key is a string and the value is

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T15:25:35+00:00Added an answer on May 11, 2026 at 3:25 pm

    A Solution in C#

    Here is a solution using the aggregate extension method:

    string result = values.Aggregate('',                   (keyString, pair) =>                     keyString + '\n' + pair.Key + ':'                       + pair.Value.Aggregate('',                           (str, val) => str + '\n\t' + val)                   ); 

    There is no LINQ syntax for the aggregate clause in C# (but apparently there is in Visual Basic for some predefined functions).

    This solution might look somewhat complex, but the aggregate method is quite useful.

    How Aggregate works

    It works like this: If you have a List<int> you can combine all values into a single aggregate value.

    That is in contrast to the Select method, which doesn’t modify the length of the list. Or the Where method, which does shrink the list, but it still remains a list (instead of a single value).

    For example, if you have a list {1, 2, 3, 4} you can combine them into a single value like this:

    int[] xs = {1, 2, 3, 4}; int sum = xs.Aggregate(0, (sumSoFar, x) => sumSoFar + x); 

    So you give two values to the Aggregate method; a seed value and a ‘combiner’ function:

    • You start aggregating with a single seed value (0 in this case).
    • Your combiner function gets called for each value in the list.
      The first argument is the computed result so far (zero the first time, later this will have other values), and it combines it with the value x.

    That’s in short how the Aggregate method works on List<int>. It works the same on KeyValuePair<string, List<string>>, but just with different types.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does VBA have dictionary structure? Like key<>value array?
I have a dictionary of 200,000 items (the keys are strings and the values
I have a known list of strings like the following: List<string> groupNames = new
I have an object that contains a property: public Dictionary<string, List<Hotel>> CityHotels { get;
I have a list of strings (a List<String> ) that can have anywhere from
A bunch of key/value pairs, from an object that may have duplicate keys, need
I have a Dictionary that is declared thusly: Dictionary myDictionary<string, List<FCPort>> = new Dictionary<string,
I have a Dictionary<int, string> which I want to take the Key collection into
I have a sorted dictionary where the key is a date and the value
I have a dictionary that I normally access with a key, so I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.