title: Entity Framework - "All" method
body: <p>The <strong>All</strong> method is supposed to evaluate the argument against all elements in the list. It works OK in regular Linq but when I try to use it with EF it throws an error ("<em>Unable to create a constant value of type 'Closure type'. Only primitive types (for instance Int32, String and Guid) are supported in this context.</em>  ")</p>
  
  <p>Example:</p>
  
  <pre><code>var myList = from person in entities.People
               where searchList.All(arg =&gt; arg == arg).ToList();
  </code></pre>
  
  <p><em>(arg == arg here is just to illustrate my question)</em></p>
  
  <p>In my scenario, searchList is a List containing search items, such as "John", "Accounting", "75". In my EF query I want to retrieve all records in People which John, Accounting and 75 appear in some specified searchable fields. A more realistic example would be something like this:</p>
  
  <pre><code>where SearchList.All((person.FirstName + " " + person.LastName + " " + person.DepartmentName + " " + person.Phone).Contains)
  </code></pre>
  
  <p>The second example also works OK with Linq, in memory, but EF doesn't like it.</p>
  
  <p>Please help! What can I do to make it work? </p>
  
  <p>This is a more specific question derived from <a href="https://stackoverflow.com/questions/879411/entity-framework-unable-to-create-a-constant-value-of-type-closure-type-er">another question</a> of mine.</p>
  
  <h2>Sample code:</h2>
  
  <pre><code>IEnumerable&lt;string&gt; 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;
  </code></pre>