title: Generic expression for where clause - "The LINQ expression node type 'Invoke' is not supported in LINQ to Entities."
body: <p>I am trying to write a really generic way to load EF entities in batches, using the Contains method to generate a SQL IN statement.  I've got it working if I pass the entire expression in, but when I try to build the expression dynamically, I am getting a "The LINQ expression node type 'Invoke' is not supported in LINQ to Entities."  So I know this means that EF thinks I'm calling an arbitrary method and it can't translate it into SQL, but I can't figure out how to get it to understand the underlying expression.</p>
  
  <p>So If I do something like this (just showing the relevant snippets):</p>
  
  <p>Function declaration:</p>
  
  <pre><code>public static List&lt;T&gt; Load&lt;T&gt;(IQueryable&lt;T&gt; entityQuery, int[] entityIds, Func&lt;T, int&gt; entityKey, int batchSize = 500, Func&lt;T, bool&gt; postFilter = null) where T : EntityObject
  {
      var retList = new List&lt;T&gt;();
  
      // Append a where clause to the query passed in, that will use a Contains expression, which generates a SQL IN statement.  So our SQL looks something like
      // WHERE [ItemTypeId] IN (1921,1920,1922)
      // See http://rogeralsing.com/2009/05/21/entity-framework-4-where-entity-id-in-array/ for details
      Func&lt;int[], Expression&lt;Func&lt;T, bool&gt;&gt;&gt; containsExpression = (entityArray =&gt; (expr =&gt; entityArray.Contains(entityKey(expr))));
  
      // Build a new query with the current batch of IDs to retrieve and add it to the list we are returning
      newQuery = entityQuery.Where&lt;T&gt;(containsExpression(entityIds));
      retList.AddRange(newQuery.ToList());
  
      return retList;
  } 
  </code></pre>
  
  <p>Call function:</p>
  
  <pre><code>var entities = BatchEntity.Load&lt;ItemType&gt;(from eItemType in dal.Context.InstanceContainer.ItemTypes
  select eItemType
  , itemTypeData
  , (ek =&gt; ek.ItemTypeId)
  );
  </code></pre>
  
  <p>I get "The LINQ expression node type 'Invoke' is not supported in LINQ to Entities."</p>
  
  <p>But if I change it to be this:</p>
  
  <p>Function declaration:</p>
  
  <pre><code>public static List&lt;T&gt; Load&lt;T&gt;(IQueryable&lt;T&gt; entityQuery, int[] entityIds, Func&lt;int[], Expression&lt;Func&lt;T, bool&gt;&gt;&gt; containsExpression, int batchSize = 500, Func&lt;T, bool&gt; postFilter = null) where T : EntityObject
  {
      var retList = new List&lt;T&gt;();
  
      // Build a new query with the current batch of IDs to retrieve and add it to the list we are returning
      newQuery = entityQuery.Where&lt;T&gt;(containsExpression(entityIds));
      retList.AddRange(newQuery.ToList());
  
      return retList;
  } 
  </code></pre>
  
  <p>Call function:</p>
  
  <pre><code>var entities = BatchEntity.Load&lt;ItemType&gt;(from eItemType in dal.Context.InstanceContainer.ItemTypes
  select eItemType
  , itemTypeData
  , (entityArray =&gt; (ek =&gt; entityArray.Contains(ek.ItemTypeId)))
  );
  </code></pre>
  
  <p>It works fine.  Is there any way I can make EF understand the more generic version?</p>