I'm using Enumerable.Except() to exclude skipSerialNumbers items from activatedSerialNumbers.
activatedSerialNumbers = activatedSerialNumbers
.Except(skipSerialNumbers, new SamWithLicenseComparer()).ToList();
SamWithLicenseComparer is:
internal class SamWithLicenseComparer : IEqualityComparer<SamWithLicense>
{
public bool Equals(SamWithLicense x, SamWithLicense y)
{
if (ReferenceEquals(x, y))
return true;
if (ReferenceEquals(x, null) || ReferenceEquals(y, null))
return false;
if(x.Name.ToLower() != y.Name.ToLower())
return false;
return true;
}
public int GetHashCode(SamWithLicense sam)
{
if (ReferenceEquals(sam, null))
return 0;
return sam.Name == null ? 0 : sam.Name.ToLower().GetHashCode();
}
}
As a result I get unexpected value, because, as I found out, items from activatedSerialNumbers compare with themselves. But the logic is that they may have the same names, task is just remove all items from skipSerialNumbers. How to do that avoiding an extra comparison?
Except
is a set operation so the result will contain always distinct values.
For example if you do {A,B,A,C}.Except({B,C})
you'll get {A}
, not {A, A}
.
You can try this:
var skipSesialNumbersSet = new HashSet<SamWithLicense>(skipSerialNumbers, new SamWithLicenseComparer());
activatedSerialNumbers = activatedSerialNumbers.Where(x => !skipSesialNumbersSet.Contains(x)).ToList();