LINQ group by 3 columns but display all of them


I have a table…

And I want to group by Col2 and Col9
And the output should be


(From d In dt.AsEnumerable
Group d By k1=d("Col1").toString.Trim, k2=d("Col2").toString.Trim Into grp=Group
Let ra = New Object(){k1,k2, ? ? ? ? ? }
Select test.Rows.Add(ra)).CopyToDataTable

I ve tried the code above only with k1 and k2… but i need all columns :frowning:

its depending on how you have decide from which group member to take the value
in case of taking it from first group member we would use for ?

@ppr yeah! first would be nice :laughing: so… let me give a shot.

but keep in mind your output

looks more as it is taking the distinct rows, where the distinct value evaluation is done over all row columns

1 Like

yes, you are totally right. to avoid losing distinct data - those columns need to be included in the group by. Correct?

we can do:

  • using remove duplicate row activity
  • YourDataTableVar.DefaultView.ToTable(True, {ColNameFirst,ColNameLast})
  • LINQ

Also have a look here:

1 Like

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