排除要删除的集合,Linq
Exclude collection for removal, Linq
我正在尝试让 except 方法像这个答案一样工作:
第一次尝试:
var housesToRemove = db.Houses.Except(houses).ToList();
第二次尝试:
var housesToRemove = db.Houses.Where(h =>
!houses.Any(h2 => h2.Code.Equals(h.Code) && h2.OperatorId == h.OperatorId)).ToList();
我的房子 class 已覆盖等于:
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();
}
两个选项都抛出:
System.NotSupportedException: 'Unable to create a constant value of type 'House'. Only primitive types or enumeration types are supported in this context.'
我使用的部分代码:
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();
}
您链接的问题是使用 Linq to Entities 而您正在使用 Linq to SQL 这意味着您的 Except
查询正在发送到数据库。数据库不知道您在这里实际尝试做什么。
我建议按照此处的答案进行操作: 了解有关失败原因以及解决方法的更多信息。
tl;dr
You're trying to compare in-memory objects to database objects, as the SQL engine doesn't give a damn about your Equals override, all it sees is an in-memory House object and a call to the database that it somehow has to be able to do the comparison via. You could instead lift the IDs of the houses you don't want and do something like:
var ids = houses.Select(h => h.Id).ToArray();
var filtered = db.Houses.Where(h => !ids.Contains(h.Id)).ToList();
我正在尝试让 except 方法像这个答案一样工作:
第一次尝试:
var housesToRemove = db.Houses.Except(houses).ToList();
第二次尝试:
var housesToRemove = db.Houses.Where(h =>
!houses.Any(h2 => h2.Code.Equals(h.Code) && h2.OperatorId == h.OperatorId)).ToList();
我的房子 class 已覆盖等于:
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();
}
两个选项都抛出:
System.NotSupportedException: 'Unable to create a constant value of type 'House'. Only primitive types or enumeration types are supported in this context.'
我使用的部分代码:
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();
}
您链接的问题是使用 Linq to Entities 而您正在使用 Linq to SQL 这意味着您的 Except
查询正在发送到数据库。数据库不知道您在这里实际尝试做什么。
我建议按照此处的答案进行操作: 了解有关失败原因以及解决方法的更多信息。
tl;dr
You're trying to compare in-memory objects to database objects, as the SQL engine doesn't give a damn about your Equals override, all it sees is an in-memory House object and a call to the database that it somehow has to be able to do the comparison via. You could instead lift the IDs of the houses you don't want and do something like:
var ids = houses.Select(h => h.Id).ToArray();
var filtered = db.Houses.Where(h => !ids.Contains(h.Id)).ToList();