I am trying to get the except method to work like in this answer: https://stackoverflow.com/a/3944821/169714
1st try:
var housesToRemove = db.Houses.Except(houses).ToList();
2nd try:
var housesToRemove = db.Houses.Where(h =>
!houses.Any(h2 => h2.Code.Equals(h.Code) && h2.OperatorId == h.OperatorId)).ToList();
My Houses class has override Equals:
public override bool Equals(object obj)
{
var item = obj as House;
if (item == null)
return false;
return this.Code == item.Code && this.OperatorId == item.OperatorId;
}
public override int GetHashCode()
{
return this.ID.GetHashCode();
}
both options throw:
System.NotSupportedException: 'Unable to create a constant value of type 'House'. Only primitive types or enumeration types are supported in this context.'
Part of the code where I use it:
using (var db = new HouseContext())
{
var housesToRemove = db.Houses.Where(h => !houses.Any(h2 => h2.Code.Equals(h.Code) && h2.OperatorId == h.OperatorId)).ToList();
foreach (var htr in housesToRemove)
{
Console.WriteLine(htr); // I have also overwritten ToString() from House
}
housesToRemove.ForEach(x => x.IsListed = false);
db.SaveChanges();
}