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?
CodePudding user response:
Upper or lower all typecodes for comparison purpose
GroupBy(r => new { TypeCode = r.TypeCode.ToUpper(), r.InterfaceID, r.ProviderID })
CodePudding user response:
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).
