title: Filtering "include" entities in EF Core
body: <p>I have the following models </p>
  
  <pre><code> public class Person
  {
      public int PersonId { get; set; }
      public string Name { get; set; }
      public List&lt;PersonRole&gt; PersonRoles { get; set; }
  
  }
  
  public class RoleInDuty
  {
      public int roleInDutyId { get; set; }
      public string Name { get; set; }
      public int typeOfDutyId { get; set; }
      public TypeOfDuty typeOfDuty { get; set; }
      public List&lt;PersonRole&gt; PersonRoles { get; set; }
  
  }
  public class PersonRole
  {
      public int PersonId { get; set; }
      public Person Person { get; set; }
      public int RoleInDutyId { get; set; }
      public RoleInDuty RoleInDuty { get; set; }
  }
  </code></pre>
  
  <p>And now I can load all people with all their roles using the following code:</p>
  
  <pre><code> var  people = _context.Persons
        .Include(p =&gt; p.PersonRoles)
          .ThenInclude(e =&gt; e.RoleInDuty).ToList();
  </code></pre>
  
  <p>But I wantn't load all data to List, I need load PersonRole according entered typeOfDutyId.
  I am trying to solve this with the following code</p>
  
  <pre><code>people = _context.Persons
    .Include(p =&gt; p.PersonRoles
      .Where(t=&gt;t.RoleInDuty.typeOfDutyId == Id)).ToList();
  </code></pre>
  
  <p>But VS throw error </p>
  
  <blockquote>
    <p>InvalidOperationException: The Include property lambda expression 'p
    => {from PersonRole t in p.PersonRoles where ([t].RoleInDuty.typeOfDutyId == __typeOfDuty_typeOfDutyId_0) select
    [t]}' is invalid. The expression should represent a property access:
    't => t.MyProperty'. To target navigations declared on derived types,
    specify an explicitly typed lambda parameter of the target type, E.g.
    '(Derived d) => d.MyProperty'. For more information on including
    related data, see <a href="http://go.microsoft.com/fwlink/?LinkID=746393" rel="noreferrer">http://go.microsoft.com/fwlink/?LinkID=746393</a>.</p>
  </blockquote>
  
  <p>As I understand I can't access property <strong>RoleInDuty.typeOfDutyId</strong> because i'm not include it yet. </p>
  
  <p>I solved this problem with the following code</p>
  
  <pre><code>people = _context.Persons
    .Include(p =&gt; p.PersonRoles)
      .ThenInclude(e=&gt;e.RoleInDuty).ToList();       
  foreach (Person p in people)
  {
    p.PersonRoles = p.PersonRoles
      .Where(e =&gt; e.RoleInDuty.typeOfDutyId == Id)
      .ToList();
  }
  </code></pre>