Linq group by

Hi all,
I’m trying to use the “group by” function through a linq expression.

I would like to group all the rows that present the values of two equal columns.

dataTable new_dt =
old_dt.AsEnumerable().GroupBy(Function(i) i.Field(Of String)(“columnWithDuplic”)).Select(Function(g) g.First).CopyToDataTable

Now, i’ve found this linq, it seems to work for one column, in which way I can add the control also on the second column?
Thx in advance

Hi,

Can you try the following?

old_dt.AsEnumerable().GroupBy(Function(r) Tuple.Create(r("column1"),r("column2"))).Select(Function(g) g.First).CopyToDataTable

OR

old_dt.AsEnumerable().GroupBy(Function(r) Tuple.Create(r("column1").ToString,r("column2").ToString)).Select(Function(g) g.First).CopyToDataTable

Regards,

1 Like

Thanks Yoichi, it works fine.

I marked it as a solution.

Just a question, what does the tuple.create command do?

Hi,

Tuple is a class which has multiple items, and can be key which is based not reference but content.

Regards

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