【发布时间】:2009-05-18 21:52:43
【问题描述】:
All 方法应该针对列表中的所有元素评估参数。它在常规 Linq 中可以正常工作,但是当我尝试将它与 EF 一起使用时,它会引发错误(“无法创建类型为“闭包类型”的常量值。只有原始类型(例如 Int32、String 和 Guid)是在这种情况下支持。 ")
例子:
var myList = from person in entities.People
where searchList.All(arg => arg == arg).ToList();
(arg == arg 这里只是为了说明我的问题)
在我的场景中,searchList 是一个包含搜索项的列表,例如“John”、“Accounting”、“75”。在我的 EF 查询中,我想检索人中 John、Accounting 和 75 出现在某些指定的可搜索字段中的所有记录。一个更现实的例子是这样的:
where SearchList.All((person.FirstName + " " + person.LastName + " " + person.DepartmentName + " " + person.Phone).Contains)
第二个例子也适用于 Linq,在内存中,但 EF 不喜欢它。
请帮忙!我该怎么做才能让它发挥作用?
这是一个来自我的another question 的更具体的问题。
示例代码:
IEnumerable<string> searchList = ParseSearchText(searchText); //search text is broken into search tokens - each token is an element in searchList. For instance "John", "Sales", "654"
var peopleQuery = from person in entities.vSearchPeople
where upperSearchList.All((person.FirstName + " " + person.Lastname + " " + person.Phone).ToUpperInvariant().Contains)
select person;
【问题讨论】:
标签: linq entity-framework linq-to-entities