使用SequenceEqual可枚举方法的LINQ to Entities错误

Mat*_*cey 4 linq linq-to-entities c#-4.0

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

            var foundStation = (from wd in _Context.AssignmentWizardDatas
                from station in wd.AssignmentWizardStationDatas
                where station.Id == stationId
                where station.Version.SequenceEqual(version)
                select station).SingleOrDefault();
Run Code Online (Sandbox Code Playgroud)

运行上面的代码时遇到以下错误:

LINQ to Entities无法识别方法'Boolean SequenceEqual [Byte](System.Collections.Generic.IEnumerable 1[System.Byte], System.Collections.Generic.IEnumerable1 [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();
Run Code Online (Sandbox Code Playgroud)

Bro*_*ass 8

您解释错误的方式是正确的,但您根本不需要SequenceEquals()- 您可以直接比较varbinary(MAX)DB中提供的字节数组.

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

  • @MattStacey:这是有道理的 - 在你的单元测试`SequenceEqual()`将在两个字节数组上工作,但如果你直接比较你只比较引用,所以它们永远不会匹配.在没有实际命中数据库的情况下对EF进行单元测试是非常困难的,因为它们需要考虑很多差异. (3认同)