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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:07:47+00:00 2026-06-16T00:07:47+00:00

I have a DataTable with 8 fields. I want to return another DataTable that

  • 0

I have a DataTable with 8 fields. I want to return another DataTable that groups the first 7 fields and has the max value of the 8th. All fields are strings, the 8th is a number stored in a string field, e.g. “24”.

Code that I’ve tried so far:

public DataTable highestVersion(DataTable dt)
        {
            DataTable dtResult=dt.Clone();

            var query=from dtRow in dt.AsEnumerable()
                      group dtRow by new
                      {
                          b_r = dtRow["r"],
                          b_1 = dtRow["b_1"],
                          b_2 = dtRow["b_2"],
                          p_r = dtRow["p_r"],
                          p_1 = dtRow["p_1"],
                          p_2 = dtRow["p_2"],
                          p_f = dtRow["p_f"]
                      }
                          into maxVersion
                          select maxVersion.OrderByDescending(a => a["p_v"]).First();

            foreach (var result in query)
            {
                 dtResult.ImportRow(result);
            }
            return dtResult;
        }

My expectation is that the group dtRow by new{} should group by the first 7 and get only the highest element p_v of each grouping by calling OrderByDescending().First(). But this doesn’t seem to actually do anything; all of the input rows are being returned.

EDIT: I just realized what the problem is. The values in p_f are different from one another, e.g.

datarow 1
--------
r: "abc"
b_1: "def"
b_2: "ghi"
p_r: "jkl"
p_1: "mno"
p_2: "pqr"
p_f: "stu_this"
p_v: "18"

datarow 2
--------
r: "abc"
b_1: "def"
b_2: "ghi"
p_r: "jkl"
p_1: "mno"
p_2: "pqr"
p_f: "stu_that"
p_v: "24"

In this case I would want to only return DataRow 2, because 24>18, and be able to retrieve the stu_that value.

  • 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-16T00:07:48+00:00Added an answer on June 16, 2026 at 12:07 am

    Updated the answer in respond to comments:

    var res = dt.AsEnumerable()
                .GroupBy(dtRow => new
                {
                    b_r = dtRow["r"],
                    b_1 = dtRow["b_1"],
                    b_2 = dtRow["b_2"],
                    p_r = dtRow["p_r"],
                    p_1 = dtRow["p_1"],
                    p_2 = dtRow["p_2"]
                })
                .Select(g => new
                {
                    Group = g,
                    Max = g.Max(r => r["p_v"])
                })
                .Select(g => new
                {
                    Key = g.Group.Key,
                    Max = g.Max,
                    Values = g.Group
                              .Where(r => r["p_v"].Equals(g.Max))
                              .Select(r => r["p_f"])
                });
    

    I guess it could be optimized to some extent, but at least this should do the job, if all the conditions are known now, of course.

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

Sidebar

Related Questions

I have a datatable with as search fields. I want a method on the
I have a Datatable that contains a column that I want to use for
I have a sql query that I return into a DataTable: SELECT TABLE_NAME, COLUMN_NAME,
I have a Kendo grid that has been sort-enabled. I want to do an
I am working with VB.NET.. i have a DataTable called QUESTION, containing 3 fields:
I have a dropdown. I need to bind with two field of datatable value
I have a Datatable in my C# program that I would like to INSERT
I have a datatable which contains yearly data saved. e.g. dtDate Value 2010-01-01 00:00:00.000
I have a datatable where I want to change the color of a cell
I have a DataTable with a DateTime column, DateCol, that can be DBNull. The

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.