实体框架 - “无法创建类型为‘闭包类型’的常量值...";错误 [英] Entity Framework - "Unable to create a constant value of type 'Closure type'..." error

查看:23
本文介绍了实体框架 - “无法创建类型为‘闭包类型’的常量值...";错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么会出现错误:

无法创建类型为闭包类型"的常量值.仅有的支持原始类型(例如 Int32、String 和 Guid)这个上下文.

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 查询时?

When I try to enumerate the following Linq query?

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

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

Update: If I try the following just to try to isolate the problem, I get the same error:

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

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

So it looks like the problem is with the All method, right? Any suggestions?

推荐答案

看起来您正在尝试执行等效于WHERE...IN"的操作健康)状况.查看 如何使用 LINQ to Entities 编写WHERE IN"样式的查询,了解如何使用 LINQ to Entities 执行此类查询的示例.

It looks like you're trying to do the equivalent of a "WHERE...IN" condition. Check out How to write 'WHERE IN' style queries using LINQ to Entities for an example of how to do that type of query with LINQ to Entities.

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

Also, I think the error message is particularly unhelpful in this case because .Contains is not followed by parentheses, which causes the compiler to recognize the whole predicate as a lambda expression.

这篇关于实体框架 - “无法创建类型为‘闭包类型’的常量值...";错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆