4

I use the code below to get the duplicated rows for 3 columns: String, Date, Money. I wonder if there is any general method that I can input a dynamic List of column name in this LINQ to find duplicated rows?

DataTable allDuplicates = dt.AsEnumerable()
    .GroupBy(dr => new
    { 
        Field1 = dr.Field<object>("String"), 
        Field2 = dr.Field<object>("Date"), 
        Field3 = dr.Field<object>("Money"), 
    })
    .Where(g => g.Count() > 1)
    .SelectMany(g => g)
    .ToList().CopyToDataTable();
}
kelly
  • 415
  • 2
  • 9
  • 24
J - C Sharper
  • 1,567
  • 7
  • 23
  • 36

2 Answers2

3

How about with a custom ArrayEqualityComparer<T> type (such as the one listed here):

string[] colsToConsider = ...

var allDuplicates = dt.AsEnumerable()
                      .GroupBy(dr => colsToConsider.Select(dr.Field<object>)
                                                   .ToArray(),
                               new ArrayEqualityComparer<object>())       
                      .Where(g => g.Count() > 1)
                      .SelectMany(g => g)
                      .CopyToDataTable();

You can also consider using a Dictionary<TKey, TValue> (and an associated dictionary-comparer) if you find the implicit use of array indices here hackish.

Community
  • 1
  • 1
Ani
  • 111,048
  • 26
  • 262
  • 307
  • Incredible! It works like a charm. You must be a master. However, it takes me a while to realize that ArrayEqualityComparer isn't a built-in .NET library, it is custom created by other from the link that you provided. – J - C Sharper Mar 01 '13 at 16:04
  • Cheers. I've added the word "custom" to my answer to make that clearer. – Ani Mar 01 '13 at 16:09
  • Awesome! exactly what I was looking for. – Himanshu Patel Feb 13 '17 at 05:00
0

while execute above code.

The type arguments for method 'System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly

Brinda
  • 1