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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:14:22+00:00 2026-06-10T06:14:22+00:00

I have a DataTable, and i want to grouped with a column . I

  • 0

I have a DataTable, and i want to grouped with a column .

I use this code for it :

DataTable  dtList = list1.Items.GetDataTable();
         var grouped = from row in dtList.AsEnumerable()
                      group row by row["T"] into valueGroup
                      select new { Value = valueGroup.Key, ID = valueGroup.Max(id => id["ID"]) };

My problem is : it is select two column(id,value). I want select multiply column.

How to select other column of dtlist ?

  • 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-10T06:14:23+00:00Added an answer on June 10, 2026 at 6:14 am

    If you want to select multiple columns to be groupped by, try something like this:

    DataTable  dtList = list1.Items.GetDataTable();
    var grouped = from row in dtList.AsEnumerable()
                          group row by new { T = row["T"], U = row["U"] } into valueGroup
                          select new { Value = valueGroup.Key, ID = valueGroup.Max(id => id["ID"]) };
    

    This will give you multiple columns returned as an anonymous type objects with T and U properties accessible in the Value field of the return set.

    I’ve tested this and it seems to work on a sample data set.

    It’s based on a solution described here: http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/54f6ac70-e74c-4d9d-a9ee-6373714c0755.

    Edit

    When using group by clause all items are divided into groups and for each group only one item is returned, so in order to refer to a column, you have to either put it in the group by clause or use an aggregation method on it (like you did with ID field and Max() method).

    However, if you want to make the groups, get the maximum ID for each group and then retrieve other columns for those items (with maximum ID value in each group) you can use a join with the same table. This code shows how to do it in your case for sample T and U columns. It seems to work correctly on a test data set:

    var dtListEnumerable = dtList.AsEnumerable();
    //group items and get max ID for each group
    var groupedTemp = from row in dtListEnumerable
              group row by row["T"] into valueGroup
              select new { Value = valueGroup.Key, ID = valueGroup.Max(id => id["ID"]) };
    
    //join to get the rest of columns for each item with max ID returned for each group
    var grouped = from groupedItem in groupedTemp
              join row in dtListEnumerable on groupedItem.ID equals row["ID"]
                  into tablesJoined
              from row in tablesJoined.DefaultIfEmpty()
              select new
              {
                  ID = groupedItem.ID,
                  T = row["T"], 
                  U = row["U"]
              };
    

    If you need any help with this, let me know. If it does not return data you want to retrieve, please give some more details about it.

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

Sidebar

Related Questions

I have this datatable That I want to have different row color for each
I have a Datatable that contains a column that I want to use for
If I have a DataTable and want to create a new row I first
I have a on memory Datatable like this. I want to produce xml file
I have a DataTable and i want to figure out the Original column value
I have a datatable and I want to filter this table with a company
I have a DataTable which contains data in this format. I want to convert
I have a DataTable. I want to get every rows first column value and
I have a datatable that I want to extract certain information from (only certain
I have a DataTable that I get from the DB, I want to create

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.