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:
@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
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();