Nebseb
Nebseb

Reputation: 39

Select entire duplicate row from datatable with linq c#

I can get it to return the single column, but I can't seem to figure out how to get it to give me the entire row instead of just the column values.

the fields it should return are, VDMList and Table.

var duplicates = dt.AsEnumerable()
    .Select(dr => dr.Field<string>("VMDList"))
    .GroupBy(x => x)
    .Where(g => g.Count() > 1)
    .Select(g => g.Key)
    .ToList();


columns of DT Table, VDMList
output is
4  | 02,2
12 | 03,3
15 | 02,2

Upvotes: 1

Views: 13317

Answers (2)

Tilak
Tilak

Reputation: 30698

Following query will return first item in the group for rows with duplicate VMDList. Note that there is no criteria for selecting the first item. It can be any random row.

var duplicates = dt.AsEnumerable()
    .GroupBy(dr => dr.Field<string>("VMDList"))
    .Where(g => g.Count() > 1)
    .Select(g => g.First())
    .ToList();

To return all rows that have duplicates, Use SelectMany

var allDuplicates = dt.AsEnumerable()
    .GroupBy(dr => dr.Field<string>("VMDList"))
    .Where(g => g.Count() > 1)
    .SelectMany(g => g)
    .ToList();

Upvotes: 3

If the requirement is selecting the entire row rather than selecting a column, then modify the linq as follows:

var duplicates = dt.AsEnumerable()
.Select(dr => dr.Field<string>("VMDList"))
.GroupBy(x => x)
.Where(g => g.Count() > 1)
.Select(g);

Where duplicates will be a group collection.

Upvotes: -1

Related Questions