title: How can I use a method to add a filter on a property in Entity Framework Core?
body: <p>I need to conditionally add a filter to particular dates in a query. There are common preconditions and the filter will be the same. Therefore I would like the common code to be in a method which can perform these checks and then have the consumer pass in the property which the filter should be applied to (could be applied to multiple).</p>
  
  <p>Here is a simplified version of my code. </p>
  
  <pre><code>var query = dbContext.Documents.AsQueryable();
  
  query = FilterDocumentsByDate(query, x =&gt; x.CreatedDate);
  query = FilterDocumentsByDate(query, x =&gt; x.SubmittedDate);
  
  private IQueryable&lt;Document&gt; FilterDocumentsByDate(IQueryable&lt;Document&gt; query, Func&lt;Document, DateTime&gt; propertyToSearch)
  {
      query = query.Where(x =&gt; propertyToSearch(x).Year &gt; 2000);
      return query;
  }
  </code></pre>
  
  <p>When I look at the query in SQL profiler, I can see that the query is missing the WHERE clause (so all documents are being retrieved and the filter is being done in memory). If I copy/paste the code inline for both dates (instead of calling the method twice) then the WHERE clause for the both dates are included in the query.</p>
  
  <p>Is there no way to add a WHERE condition to an IQueryable by passing a property in a Func which can be properly translated to SQL by Entity Framework?</p>