Need a help on Group By

Hello everyone
I trying to use the “group by” function

Its working for only on column. which way I can add the control also on the second column?

Dt.AsEnumerable().GroupBy(Function(i) i.Field(Of String)(“Col1”)).Select(Function(g) g.First).CopyToDataTable

Hi @Marian_B

Try this expression

Dt.AsEnumerable().GroupBy(Function(r) Tuple.Create(r("colName1"),r("colName2"))).Select(Function(g) g.First).CopyToDataTable

Regards
Gokul

3 Likes

Hi @Marian_B ,

I’m not entirely sure what the expected output is, but if you want to group based on two columns, here you go:

Dt.AsEnumerable().GroupBy(Function(i) Tuple.Create(i.Field(Of String)(“Col1”),i.Field(Of String)(“Col2”))).Select(Function(g) g.First).CopyToDataTable

Kind Regards,
Ashwin A.K

1 Like

Thanks for Quick reply

I need to group the Two columns @ashwin.ashok

Hi @Marian_B ,

Below is another way to Group By Two Columns :

Dt.AsEnumerable().GroupBy(Function(i) New With  {Key .col1 = i("Col1").ToString, Key .col2 = i("Col2").ToString}).Select(Function(g) g.First).CopyToDataTable
1 Like

Hi @Marian_B ,

If that is the case, then the above code should do the trick.
Is there anything specific that you are looking for?

Like an operation after Grouping data?
We can help you out there as well.

Kind Regards,
Ashwin A.K

(From d in dtData.AsEnumerable
Group d by k1=d(YourColNameOrIndex).toString.Trim, k2=d(YourColNameOrIndex).toString.Trim intogrp=Group
Select r = grp.First()).CopyToDataTable
1 Like

No, only i need to group two or more column.

I will check all the expression @ashwin.ashok @Gokul001 @supermanPunch @ppr and let you know

1 Like

Its working

Thank to all @Gokul001 @supermanPunch @ashwin.ashok @ppr

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