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

标签 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 does not recognize the method 'Boolean SequenceEqual[Byte](System.Collections.Generic.IEnumerable1[System.Byte], System.Collections.Generic.IEnumerable1[System.Byte])' method, and this method cannot be translated into a store expression.



经过一些阅读,我意识到问题是 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();

最佳答案

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

关于使用 SequenceEqual 可枚举方法的 LINQ to Entities 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5621598/

相关文章:

linq - 从 C# 调用的存储过程的执行时间是从 SQL Management studio 调用的存储过程的 6 倍

C#多文本文件处理

c# - EF Code First - 如何使用 Include 扩展方法过滤相关对象

c# - .NET - 如何比较 2 个代码块之间的性能?

mysql - 如何使用 MySQL(如 SqlBulkCopy)和 Linq to Entities 进行批量插入

c# - Linq Sum 动态参数

c# - 从自定义类对象列表中获取那些元素,这些元素的一个属性值可解析为 double

c# - 两次 Linq 调用的性能比较

c# - 重构 foreach 和 if's 到 Linq

asp.net - 查询中的 Linq 子查询