将 IEqualityComparer 与 LINQ to Entities except 子句结合使用
我有一个实体,我想将其与子集进行比较,并确定选择除子集之外的所有实体。
因此,我的查询如下所示:
Products.Except(ProductsToRemove(), new ProductComparer())
ProductsToRemove()
方法在执行一些任务后返回一个 List
。所以最简单的形式就是上面的内容。
ProductComparer()
类如下所示:
public class ProductComparer : IEqualityComparer<Product>
{
public bool Equals(Product a, Product b)
{
if (ReferenceEquals(a, b)) return true;
if (ReferenceEquals(a, null) || ReferenceEquals(b, null))
return false;
return a.Id == b.Id;
}
public int GetHashCode(Product product)
{
if (ReferenceEquals(product, null)) return 0;
var hashProductId = product.Id.GetHashCode();
return hashProductId;
}
}
但是,我不断收到以下异常:
LINQ to Entities 无法识别 方法 'System.Linq.IQueryable<代码>1[UnitedOne.Data.Sql.Product] 除了[产品](System.Linq.IQueryable1[UnitedOne.Data.Sql.Product], System.Collections.Generic.IEnumerable
1[UnitedOne.Data.Sql.Product], System.Collections.Generic.IEqualityComparer
1[UnitedOne.Data.Sql.Product])' 方法,并且该方法不能 翻译成商店表达式。
I have an entity that I'd like to compare with a subset and determine to select all except the subset.
So, my query looks like this:
Products.Except(ProductsToRemove(), new ProductComparer())
The ProductsToRemove()
method returns a List<Product>
after it performs a few tasks. So in it's simplest form it's the above.
The ProductComparer()
class looks like this:
public class ProductComparer : IEqualityComparer<Product>
{
public bool Equals(Product a, Product b)
{
if (ReferenceEquals(a, b)) return true;
if (ReferenceEquals(a, null) || ReferenceEquals(b, null))
return false;
return a.Id == b.Id;
}
public int GetHashCode(Product product)
{
if (ReferenceEquals(product, null)) return 0;
var hashProductId = product.Id.GetHashCode();
return hashProductId;
}
}
However, I continually receive the following exception:
LINQ to Entities does not recognize
the method
'System.Linq.IQueryable1[UnitedOne.Data.Sql.Product]
1[UnitedOne.Data.Sql.Product],
Except[Product](System.Linq.IQueryable
System.Collections.Generic.IEnumerable1[UnitedOne.Data.Sql.Product],
1[UnitedOne.Data.Sql.Product])'
System.Collections.Generic.IEqualityComparer
method, and this method cannot be
translated into a store expression.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Linq to Entities 实际上并不执行您的查询,而是解释您的代码,将其转换为 TSQL,然后在服务器上执行。
在幕后,它是根据运算符和常用函数如何操作以及它们如何与 TSQL 相关的知识进行编码的。问题是 L2E 的开发人员不知道您到底是如何实现 IEqualityComparer 的。因此,他们无法弄清楚当您说
Class A == Class B
时,您的意思是(例如)“Where Person.FirstName == FirstName AND Person.LastName == LastName”
。因此,当 L2E 解释器遇到它无法识别的方法时,它会抛出此异常。
有两种方法可以解决这个问题。首先,开发一个满足您的平等要求但不依赖于任何自定义方法的
Where()
。换句话说,测试实例属性的相等性,而不是测试类上定义的Equals
方法。其次,您可以触发查询的执行,然后在内存中进行比较。例如:
显然,这会通过网络带来更多的数据,并且会占用更多的内存。第一个选择通常是最好的。
Linq to Entities isn't actually executing your query, it is interpreting your code, converting it to TSQL, then executing that on the server.
Under the covers, it is coded with the knowledge of how operators and common functions operate and how those relate to TSQL. The problem is that the developers of L2E have no idea how exactly you are implementing IEqualityComparer. Therefore they cannot figure out that when you say
Class A == Class B
you mean (for example)"Where Person.FirstName == FirstName AND Person.LastName == LastName"
.So, when the L2E interpreter hits a method it doesn't recognize, it throws this exception.
There are two ways you can work around this. First, develop a
Where()
that satisfies your equality requirements but that doesn't rely on any custom method. In other words, test for equality of properties of the instance rather than anEquals
method defined on the class.Second, you can trigger the execution of the query and then do your comparisons in memory. For instance:
Obviously this will bring much more data across the wire and be more memory intensive. The first option is usually the best.
您正在尝试使用自定义
IEqualityComparer
将Except
调用转换为 Entity SQL。显然,你的类无法转换为SQL。
您需要编写
Products.AsEnumerable().Except(ProductsToRemove(), new ProductComparer())
来强制它在客户端上执行。请注意,这将从服务器下载所有产品。顺便说一句,您的
ProductComparer
类应该是单例,如下所示:You're trying to convert the
Except
call with your customIEqualityComparer
into Entity SQL.Obviously, your class cannot be converted into SQL.
You need to write
Products.AsEnumerable().Except(ProductsToRemove(), new ProductComparer())
to force it to execute on the client. Note that this will download all of the products from the server.By the way, your
ProductComparer
class should be a singleton, like this:IEqualityComparer
只能在本地执行,无法转换为 SQL 命令,因此会出现错误The
IEqualityComparer<T>
can only be executed locally, it can't be translated to a SQL command, hence the error