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

时间:2011-04-11 12:52:53

标签: linq c#-4.0 linq-to-entities

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

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

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

  

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();

3 个答案:

答案 0 :(得分:8)

您解释错误的方式是正确的,但您根本不需要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();

答案 1 :(得分:4)

//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; 

答案 2 :(得分:1)

9 年之后……除了 SequenceEqualLength 之外,对 Contains 的 LINQ 支持已添加到 EF Core 5.0。

MS Docs