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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 89k
  • Answers 89k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You are loading an entire HTML page into your div,… May 11, 2026 at 5:51 pm
  • Editorial Team
    Editorial Team added an answer The Front Controller pattern defines a single component that is… May 11, 2026 at 5:50 pm
  • Editorial Team
    Editorial Team added an answer Because it is a pointer to foo. And foo has… May 11, 2026 at 5:50 pm

Related Questions

I have a Dictionary<string,int> that has the potential to contain upwards of 10+ million
I'm trying to read input from the terminal. For this, I'm using a BufferedReader.
I am using a PropertyGrid to show properties from my objects. However, I'm also
I need to store products for an e-commerce solution in a database. Each product
I'm trying to implement something like Google suggest on a website I am building

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.