I have two lists of my own class, and I'm trying to get all the items in one that aren't in the other.
//List<CADataType> ConsoleDataList is a list of 1071 CADataTypes
//List<CADataType> ComDataList is a list of 2153 CADataTypes
//This list ends up containing 1144 items
List<CADataType> intersectLoose = (from console in ConsoleDataList
join com in ComDataList
on console.CompareField equals com.CompareField
select console).ToList();
//Therefore there are 1144 items that are in both lists
//This SHOULD result in 2153 - 1144 = 1009 items
List<CADataType> remove = ComDataList.Except(ConsoleDataList).ToList(); //<-- Problem is here
//Ends up containing 2153 items
CADataType is actually an abstract class, and all lists actually have CAPropertyData. This is the Equals class from CAPropertyData:
public override bool Equals(CADataType obj)
{
if (!(obj is CAPropertyData))
return false;
CAPropertyData y = (CAPropertyData)obj;
if (this.PropertyAddress.ToString() == y.PropertyAddress.ToString()) //Breakpoint here
return true;
return false;
}
I've put a breakpoint on that last if statement in the Equals, and it hits the breakpoint correctly when I step into the Except line, AND I can see that this.PropertyAddress.ToString()
DOES equal y.PropertyAddress.ToString()
in some cases, so it's finding some equal objects, but it doesn't seem to exclude them?
Any idea what's wrong?
EDIT: intersectLoose
is there to show you that there are matching CADataTypes in both lists.
CompareField is defined and using the same property as the Equals method:
public override string CompareField => PropertyAddress.ToString();