How to get max value in a column using Group by in Linq Query? Please Help

MaxValDT.xaml (7.4 KB) I need to get max value in a column based on Group by of two columns in datatable

From the Source Datatable, I need to get max values of column “Col 3” (found as string in SourceDT) based on Group by columns “Col 1” and “Col 2” in a new datatable. And “Col 4” is misc data.
Please help me to solve this.

What I have:
Source Datatable:

What I need:
In a New Datatable (DT1):

Since dealing with datatable with large data, I am trying in LinQ Query instead of For Each loop:

Please help me

find starter help here:
MaxValDT.xaml (8.8 KB)

@ppr Thanks for the reply.
But for Col 4, I don’t want all group by values. I want only matched value as mentioned in the screenshot.
Currently, in the output DT, Col 4 is having all values with comma separated (ex: X1, Y2). But I want only matched Col 4 value (ex: Y2).
Please help me

was not realized, will have a look on. Kindly note if two rows will have the same max val then it will implemented to take any.will update soon

find updated xaml:
MaxValDT.xaml (8.8 KB)

1 Like

Here is the LINQ query in C#

var output = from row in SourceDT.AsEnumerable()
            group row by new {Col1 = row.Field<string>("Col 1"), Col2 = row.Field<int>("Col 2"), Col4 = row.Field<string>("Col 4")} into grp 
            select grp.OrderByDescending(Col3 => row.Field<int>("Col 3").FirstOrDefault();

Karthik Byggari

@ppr Thanks a lot for the solution.
It is working as expected.
Thanks again.

@KarthikByggari Thanks for the reply.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.