title: How to perform a conditional include in Entity Framework
body: <p>I'm currently using EF and I have two tables, <code>Employees</code> and <code>Orders</code>, given that an <code>Employee</code> have multiple <code>Orders</code>.</p>
  
  <p>I need to perform a query in EF that brings me a list of all <code>Employees</code> with their related <code>Orders</code>, but only those between two dates. If an <code>Employee</code> didn't perform any <code>Order</code> in the given period, his <code>Orders</code> list will be empty.</p>
  
  <p>I figured that I need to start my query from the <code>Employees</code> DbSet, but how can I assign a condition to the <code>Orders</code> property?</p>
  
  <p>I tried to do this, but obviously it didn't work.</p>
  
  <pre><code>public List&lt;Employee&gt; GetAllByIdListAndDateRange(int[] ids, DateTime fromDate, DateTime toDate)
      {
          var query = _context.Set&lt;Employee&gt;().Where(a =&gt; ids.Contains(a.EmployeeID)).Include(a =&gt; a.Orders.Where(o.OrderDate &lt;= toDate &amp;&amp; o.OrderDate &gt;= fromDate));
  
          return query.ToList();
      }
  </code></pre>
  
  <p>Any help? I guess I might be missing out on something really simple here.</p>