c# - LINQ to Entities 无法识别方法 'Boolean Contains(Int32)' 方法

标签 c# linq entity-framework

<分区>

下面的异常发生了

LINQ to Entities does not recognize the method 'Boolean Contains(Int32)' method, and this method cannot be translated into a store expression.

在尝试执行以下查询时

List<int> studentIDs = Common.getFilterStudents();
var query = from a in studentTable
            where studentIDs.Contains(a.StudentID)
            select a;

如何使用学生 ID 列表过滤查询?

最佳答案

无法在您的 EF 版本中使用 Contains,因为在 EF 4 中添加了对 Contains 的支持。升级您的 EF 版本(然后您的代码将起作用没有任何问题),或使用类似 manual expression building 的东西:

var query = context.studentTable.Where(
    BuildContainsExpression<Student, int>(s => s.StudentID, studentIDs));

关于c# - LINQ to Entities 无法识别方法 'Boolean Contains(Int32)' 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17940048/

相关文章:

c# - 如何更改 VS2015 中的代码生成策略?

c# - 每个用户的 Entity Framework 行级安全性

如果 XML 文件附加了模式,c# XSLT 转换不起作用?

c# - 设置一个 zip 文件到 redis 缓存

c# - LINQ 沿着对象树查询需要匹配的值

c# - 根据 LINQ/lambda 中的 Group By 语句计数创建计数

entity-framework - 解决 EF4 中的实体与我们的数据库标准之间的命名约定冲突?

c# - C# 中的复制构造函数仍然保留其引用

C#反序列化JSON

c# - 使用 LINQ 延迟分区序列