Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用SequenceEqual枚举方法LINQ到实体错误_Linq_C# 4.0_Linq To Entities - Fatal编程技术网

使用SequenceEqual枚举方法LINQ到实体错误

使用SequenceEqual枚举方法LINQ到实体错误,linq,c#-4.0,linq-to-entities,Linq,C# 4.0,Linq To Entities,我有以下LINQ语句(stationId是一个int,version是一个字节数组): 运行上述代码时,我遇到以下错误: LINQ to Entities无法识别方法“Boolean SequenceEqual[Byte](System.Collections.Generic.IEnumerable1[System.Byte],System.Collections.Generic.IEnumerable1[System.Byte]),此方法无法转换为存储表达式 经过一段时间的阅读,我意识到问题在

我有以下LINQ语句(stationId是一个int,version是一个字节数组):

运行上述代码时,我遇到以下错误:

LINQ to Entities无法识别方法“Boolean SequenceEqual[Byte](System.Collections.Generic.IEnumerable
1[System.Byte],System.Collections.Generic.IEnumerable
1[System.Byte]),此方法无法转换为存储表达式

经过一段时间的阅读,我意识到问题在于LINQ无法将SequenceEqual方法调用转换为SQL等效方法(我认为无论如何)。有人知道这方面的解决方案吗?或者,当我尝试在LINQ查询中使用字节数组时,也许可以为我指出正确的方向,但我找不到一个专门针对字节数组的现有问题

提前谢谢

编辑:使用Jani的帖子,这是用于解决此错误的最终代码:

        //eager execution of the linq query to find the stations 
   var foundStation = (from wd in _Context.AssignmentWizardDatas
                            from station in wd.AssignmentWizardStationDatas
                            where station.Id == stationId
                            select station).ToList();
   //finding the result in memory   
        var result = (from f in foundStation
                      where f.Version.SequenceEqual(version)
                      select f).SingleOrDefault();

您解释错误的方式是正确的,但根本不需要
SequenceEquals()
——只要绑定到数据库中的
varbinary(MAX)
,就可以直接比较字节数组

var foundStation = (from wd in _Context.AssignmentWizardDatas
from station in wd.AssignmentWizardStationDatas
where station.Id == stationId
where station.Version == version
select station).SingleOrDefault();

9年后,但是。。。除了
Length
Contains
之外,EF Core 5.0还添加了对
SequenceEqual
的LINQ支持


请参阅。

是的,当我运行该网站时,您是对的。该网站按预期运行。我在一个TDD环境中工作,现在有点困惑,因为我最初使用了这段代码,但更改了它,因为它会导致我的单元测试失败,而使用SequenceEqual方法实际上通过了单元测试,但在运行时遇到异常。你知道为什么会这样吗?@MattStacey:这很有道理-在单元测试中,
SequenceEqual()
将在两字节数组上工作,但如果直接比较,则只比较引用,因此它们永远不会匹配。很难对EF进行单元测试,因为它们之间存在很多差异,我已经验证了这一点。我们使用NHibernate、Fluent和Linq2NH遇到了相同的问题。转换为一个简单的“e.ssn==ssn”,其中ssn都是varbinary和byte[],它开始工作。我假设Linq2NH理解比较并将其转换为适当的HQL,HQL理解映射+参数类型组合并转换为适当的SQL。这在nHibernate中(现在)不起作用。使用entity.ByteArrayProperty.Equals(somebyteArray)或entity.ByteArrayProperty==somebyteArray都会失败,并出现querysyntax/MismatchedReenodeException。我可以从发出的hql中看出,nHibernate试图创建一个x IN(,)表达式,其中字节数组中的所有值都成为IN表达式的参数。不过这对QueryOver有效,但不是linq。谢谢Jani,你试过吗?对我来说,这仍然会引发相同的异常?但当您将代码更改为此时,可能记录的异常与
LINQ to Entities
无关,它将在内存中执行。你确定吗?谢谢。我又看了一遍,我想我没有在第一条LINQ语句的末尾包含.ToList()。我已经更新了我的原始问题,以显示我使用的最终答案,因为它与您在这里所写的略有不同。谢谢你的帮助。@MattStacey:你走错了路——改变查询EF的方式,将所有结果具体化到内存中,只是为了让单元测试通过,这是一种非常非常糟糕的方法。不要期望它能够扩展,即使它现在在技术上“起作用”。
//eager execution of the linq query to find the stations
var foundStation = (from wd in _Context.AssignmentWizardDatas
                    where wd.Id == stationId
                    select station).ToList();
//finding the result in memory  
var result = from f in foundStation 
             where f.version.SequenceEqual(version)
             select f; 
var foundStation = (from wd in _Context.AssignmentWizardDatas
from station in wd.AssignmentWizardStationDatas
where station.Id == stationId
where station.Version == version
select station).SingleOrDefault();