title: How to use a Func in an expression with Linq to Entity Framework?
body: <p>I am trying to write a linq to entity extension method that takes a Func to select a property Id and compare it against a list of ids.</p>
  
  <p><strong>Classes</strong></p>
  
  <pre><code>public class A
  {
      public int AId { get; set; }
  }
  
  public class B
  {
      public int BId { get; set; }
  }
  </code></pre>
  
  <p><strong>Extension Method</strong></p>
  
  <pre><code>public static IQueryable&lt;T&gt; WithId&lt;T&gt;(this IQueryable&lt;T&gt; entities,
      Func&lt;T, int&gt; selector, IList&lt;int&gt; ids)
      {
          Expression&lt;Func&lt;T, bool&gt;&gt; expression = x =&gt; ids.Contains(selector(x));
          return entities.Where(expression); // error here (when evaluated)
      }
  </code></pre>
  
  <p><strong>Calling Method</strong></p>
  
  <pre><code>var ids = new List&lt;int&gt; { 1, 2, 3 };
  DbContext.EntityAs.WithId(e =&gt; e.AId, ids);
  DbContext.EntityBs.WithId(e =&gt; e.BId, ids);
  </code></pre>
  
  <p>The problem I am experiencing is that it is trying to Invoke the function which is not allowed in Entity Framework.</p>
  
  <p>How can I use a property selector (Func) to evaluate the query?</p>