gpt4 book ai didi

linq - Entity Framework - "Unable to create a constant value of type ' 闭包类型'...”错误

转载 作者:行者123 更新时间:2023-12-03 05:14:39 26 4
gpt4 key购买 nike

为什么我会收到错误:

Unable to create a constant value of type 'Closure type'. Only primitive types (for instance Int32, String and Guid) are supported in this context.

当我尝试枚举以下 Linq 查询时?

IEnumerable<string> searchList = GetSearchList();
using (HREntities entities = new HREntities())
{
var myList = from person in entities.vSearchPeople
where upperSearchList.All( (person.FirstName + person.LastName) .Contains).ToList();
}

更新:如果我尝试以下操作只是为了尝试隔离问题,我会收到相同的错误:

where upperSearchList.All(arg => arg == arg) 

看来问题出在 All 方法上,对吗?有什么建议吗?

最佳答案

看起来您正在尝试执行相当于“WHERE...IN”条件的操作。查看How to write 'WHERE IN' style queries using LINQ to Entities有关如何使用 LINQ to Entities 执行此类查询的示例。

此外,我认为在这种情况下错误消息特别没有帮助,因为 .Contains 后面没有括号,这会导致编译器将整个谓词识别为 lambda 表达式。

关于linq - Entity Framework - "Unable to create a constant value of type ' 闭包类型'...”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/879411/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com