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

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

问题描述

为什么我会收到错误:


无法创建Closure类型类型的常量值。
原始类型(例如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天全站免登陆