views:

4198

answers:

2

Why do I get the error "Unable to create a constant value of type 'Closure type'. Only primitive types (for instance Int32, String and Guid) are supported in this context." 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)

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

+4  A: 

It looks like you're trying to do the equivalent of a "WHERE...IN" condition. Check out this blog post for an example of how to do that type of query with LINQ to Entities.

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.

Daniel Pratt
Thanks Daniel. The same syntax works fine with plain Linq. So, it looks like to problem is with EF in .Net 3.5 SP1 right?The Contains without parenthesis is equivalent to:where upperSearchList.All(x => (person.FirstName + person.LastName) .Contains(x)).ToList();
Gustavo Cavalcanti
If I try where upperSearchList.All(arg => arg == arg) it throws the same error. So the problem is with the All method...
Gustavo Cavalcanti
LINQ to Entities is a wonderful technology, but the SQL translation engine is limited. I can't put my hands on official documentation, but in my experience, if the query includes more than just basic math and string/date functions, it's not going to work. Have you had a chance to check out that post I linked? It describes a process of converting a "WHERE..IN" type query into a form that LINQ to Entities can then translate into SQL.
Daniel Pratt
A: 

That is an known issue: Known Issues and Considerations in LINQ to Entities

Salar Khalilzadeh