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).