1

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.

Help!!

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
Nebseb
  • 39
  • 1
  • 1
  • 6

2 Answers2

3

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();
Tilak
  • 30,108
  • 19
  • 83
  • 131
  • I want it to return all the rows that have duplicates, not just the first one. – Nebseb Jan 07 '13 at 04:10
  • You need to use [SelectMany](http://msdn.microsoft.com/en-us/library/system.linq.enumerable.selectmany.aspx). Updated the answer – Tilak Jan 07 '13 at 04:12
  • How can I do this if the duplicates have to be found based on a group of unique columns? – b.g Dec 22 '16 at 12:29
  • @b.g Check out this link. http://stackoverflow.com/questions/15161180/use-linq-to-find-duplicated-rows-with-list-of-specified-columns – Himanshu Patel Feb 13 '17 at 05:20
-1

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.