1
var duplicates = wb.MyList.AsEnumerable()
    .GroupBy(r => new { r.TypeCode, r.InterfaceID,r.ProviderID})
    .Where(gr => gr.Count() > 1);

I have two TypeCode with different case one is in upper case and other is in lowercase typecode , due to case sensitive it didn't return as duplicates

I have tried with StringComparer.InvariantCultureIgnoreCase but it gave error. Any other solution?

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Sks
  • 19
  • 2
  • `but it gaves error.` - ***what error***? welcome to stackoverflow. i recommend [taking the tour](https://stackoverflow.com/tour), as well as reading [how to ask a good question](https://stackoverflow.com/help/how-to-ask) and [what's on topic](https://stackoverflow.com/help/on-topic). – Franz Gleichmann Feb 03 '22 at 10:26
  • I tried with ToUpper() but it gavescompiler error as Invalid anonymous type declartor – Sks Feb 03 '22 at 10:31

2 Answers2

2

Upper or lower all typecodes for comparison purpose

GroupBy(r => new { TypeCode = r.TypeCode.ToUpper(), r.InterfaceID, r.ProviderID })
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
fubo
  • 44,811
  • 17
  • 103
  • 137
0

A more "scientific" approach would be to implement a custom IEqualityComparer<T>, and then group your records using this comparer. The Equals method of this comparer should check for equality the properties TypeCode, InterfaceID and ProviderID of your records. The comparer could then be used like this:

.GroupBy(r => r, new CustomComparer())

If you are OK with adding third-party dependencies in your project, you could also consider installing the Nito.Comparers package, and create your comparer fluently like this:

var myComparer = Nito.Comparers.EqualityComparerBuilder
    .For<MyRecord>()
    .EquateBy(r => r.TypeCode, StringComparer.OrdinalIgnoreCase)
    .ThenEquateBy(r => r.InterfaceID)
    .ThenEquateBy(r => r.ProviderID);

var duplicates = wb.MyList
    .GroupBy(r => r, myComparer)
    .Where(g => g.Count() > 1);

You can read here why using the StringComparer class is superior to using ToUpper for case insensitive string comparisons. The reasons are quite intricate, so in your case the difference might not be important (both approaches might work fine).

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104