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 7731231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:25:45+00:00 2026-06-01T06:25:45+00:00

Using LINQ can I get the data from the table into the format shown

  • 0

Using LINQ can I get the data from the table into the format shown directly below? Can LINQ perform this type of formating or should I loop through the results to format?

Format I’m trying to get data into:

dau      gmu           population     year    
------   ----------    ----------    -------
1        2, 201        1680          2010
2        3, 4, 5, 14   14570         2010

DATA IN TABLE:

 id          dau         gmu         population         year species
----------- ----------- ----------- ------------------- ---- -------
1           1           2           1680                2010 E
2           1           201         1680                2010 E
3           2           3           14570               2010 E
4           2           4           14570               2010 E
5           2           5           14570               2010 E
6           2           14          14570               2010 E
  • 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. Editorial Team
    Editorial Team
    2026-06-01T06:25:46+00:00Added an answer on June 1, 2026 at 6:25 am

    The actual formatting will be up to you, but assuming that you have a collection of these items called list, you can do this:

    var result = from x in list
                 group x by new {x.dau, x.population, x.year} into p
                 select new 
    {
        dau = p.Key.dau,
        population = p.Key.population,
        year = p.Key.year,
        gmu = p.Select (x => x.gmu)
    
    };
    

    You may have to pull this into an in-memory list to do this, if you’re actually dealing with L2E.

    EDIT: Just tried it with L2E and it seems to work fine as written. Instead of list in your query, it will be something more like context.TableName.

    Here’s the full code. The output is very sloppy, but it’s late, and I don’t think the OP was worrying about the formatting so much:

            var result = from x in list.Stacko1
                         group x by new { x.dau, x.population, x.year } into p
                         select new
                         {
                             dau = p.Key.dau,
                             population = p.Key.population,
                             year = p.Key.year,
                             gmu = p.Select(x => x.gmu)
    
                         };
    
            result.ToList().ForEach(item=>
                                        {
                                            System.Console.Write("{0}\t\t", item.dau);
    
                                            item.gmu.ToList().ForEach(gmuElement=>
                                                                          {
                                                                              System.Console.Write("{0},", gmuElement);
                                                                          });
    
    
                                            System.Console.Write("\t\t");
                                            System.Console.Write("{0}\t\t", item.population);
                                            System.Console.WriteLine("{0}", item.year);
                                        });
    
            System.Console.ReadKey();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I retrieve data from a SQL Server using LINQ and display each
I need to get two fields from a database table (retrieved using linq-to-sql), one
How can I get the Nth row using Linq? both columns are text so
Somehow, using linq I can't test it with this CUF field in the beginning:
Using LINQ to Entities, how can I determine if any item from a List
Using EF we can use LINQ to read data which is rather simple (especially
I need to gather Distinct Id's from a particular table using LINQ. The catch
I'm simply trying to get data from two sql server db tables using ado.net
I am trying to extract some data from a website using a LINQ statement,
I am using Linq to sql concept to manipulate my data from asp.net mvc

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.