将 IEqualityComparer 与 LINQ to Entities except 子句结合使用

发布于 2024-08-19 06:28:31 字数 1223 浏览 7 评论 0原文

我有一个实体,我想将其与子集进行比较,并确定选择除子集之外的所有实体。

因此,我的查询如下所示:

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.IEnumerable1[UnitedOne.Data.Sql.Product], System.Collections.Generic.IEqualityComparer1[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]
Except[Product](System.Linq.IQueryable
1[UnitedOne.Data.Sql.Product],
System.Collections.Generic.IEnumerable1[UnitedOne.Data.Sql.Product],
System.Collections.Generic.IEqualityComparer
1[UnitedOne.Data.Sql.Product])'
method, and this method cannot be
translated into a store expression.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

余厌 2024-08-26 06:28:31

Linq to Entities 实际上并不执行您的查询,而是解释您的代码,将其转换为 TSQL,然后在服务器上执行。

在幕后,它是根据运算符和常用函数如何操作以及它们如何与 TSQL 相关的知识进行编码的。问题是 L2E 的开发人员不知道您到底是如何实现 IEqualityComparer 的。因此,他们无法弄清楚当您说Class A == Class B时,您的意思是(例如)“Where Person.FirstName == FirstName AND Person.LastName == LastName”

因此,当 L2E 解释器遇到它无法识别的方法时,它会抛出此异常。

有两种方法可以解决这个问题。首先,开发一个满足您的平等要求但不依赖于任何自定义方法的 Where()。换句话说,测试实例属性的相等性,而不是测试类上定义的 Equals 方法。

其次,您可以触发查询的执行,然后在内存中进行比较。例如:

var notThisItem = new Item{Id = "HurrDurr"};
var items = Db.Items.ToArray(); // Sql query executed here
var except = items.Except(notThisItem); // performed in memory

显然,这会通过网络带来更多的数据,并且会占用更多的内存。第一个选择通常是最好的。

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 an Equals method defined on the class.

Second, you can trigger the execution of the query and then do your comparisons in memory. For instance:

var notThisItem = new Item{Id = "HurrDurr"};
var items = Db.Items.ToArray(); // Sql query executed here
var except = items.Except(notThisItem); // performed in memory

Obviously this will bring much more data across the wire and be more memory intensive. The first option is usually the best.

贵在坚持 2024-08-26 06:28:31

您正在尝试使用自定义 IEqualityComparerExcept 调用转换为 Entity SQL。

显然,你的类无法转换为SQL。

您需要编写 Products.AsEnumerable().Except(ProductsToRemove(), new ProductComparer()) 来强制它在客户端上执行。请注意,这将从服务器下载所有产品。


顺便说一句,您的 ProductComparer 类应该是单例,如下所示:

public class ProductComparer : IEqualityComparer<Product> {
    private ProductComparer() { }
    public static ProductComparer Instance = new ProductComparer();

    ...
}

You're trying to convert the Except call with your custom IEqualityComparer 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:

public class ProductComparer : IEqualityComparer<Product> {
    private ProductComparer() { }
    public static ProductComparer Instance = new ProductComparer();

    ...
}
虐人心 2024-08-26 06:28:31

IEqualityComparer 只能在本地执行,无法转换为 SQL 命令,因此会出现错误

The IEqualityComparer<T> can only be executed locally, it can't be translated to a SQL command, hence the error

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文