LINQ get rows from datatable which is more than one

Hi!
I have datatable with 25 columns.
And first column name is ID.
How with LINQ I can get all ID, which is more than one in datatable?
Thanks!

1 Like

Do you mean duplicate ID?

Cheers

It won’t help here, as 25 columns and other columns have different values.
I just need to get all IDs with more than one record

Well, I do not understand this statement:

Maybe you could give an example

Cheers

Simple example. We have datatable
Снимок

And I must get only 1,3,5 because they more than 1 record in DT

First create list of all IDs

IDList = dt.AsEnumerable.Select(function(row) row(“ID”).ToString).ToList()

then filter out every item that occurs only once

DuplicateIDs = IDList.Distinct().Where(function(item) IDList.LastIndexOf(item) <> IDList.IndexOf(item) ).ToList()

In my example I used list of strings, but you can use ints if this suits you better.
There probably is a better way to do it, but this works for me.

Assuming you need just the duplicate IDs and the ID is string

StrArray = (From row In dt.AsEnumerable
Group row By keys = New With {
key .ID = row.field(Of String)(“ID”)}
Into gr = Group
Where gr.Count() > 1
Select keys.ID
).ToArray

Cheers

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