使用带有LINQ to Entities Except子句的IEqualityComparer

Jas*_*ord 5 linq linq-to-entities iequalitycomparer

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

所以,我的查询看起来像这样:

Products.Except(ProductsToRemove(), new ProductComparer())
Run Code Online (Sandbox Code Playgroud)

ProductsToRemove()方法List<Product>在执行一些任务后返回a .因此,它是最简单的形式,就是上面的.

这个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;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我不断收到以下异常:

LINQ to Entities无法识别方法'System.Linq.IQueryable 1[UnitedOne.Data.Sql.Product] Except[Product](System.Linq.IQueryable1 [UnitedOne.Data.Sql.Product],System.Collections.Generic.IEnumerable 1[UnitedOne.Data.Sql.Product], System.Collections.Generic.IEqualityComparer1 [UnitedOne.Data.Sql.Product])'方法,而且这个方法不能被翻译成商店表达.

小智 7

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

在封面下,它使用操作员和常用功能如何操作以及这些操作与TSQL的关系进行编码.问题是L2E的开发人员不知道你是如何实现IEqualityComparer的.因此他们无法弄清楚当你说A类== 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
Run Code Online (Sandbox Code Playgroud)

显然,这将为整个线路带来更多数据,并且会占用大量内存.第一种选择通常是最好的.