title: Include property of each item in a collection in EF Core
body: <p>I have a .Net Core 2.2 project with EntityFramework and a model with many nested properties either single object or collections.</p>
  
  <p>I've enabled lazy loading and now I want selectively load only the parts of the object tree I'm insterested in in my controller.</p>
  
  <p>When I use .Include() everything is fine until I want to include a collection property and for every item in that collection I want to include a related entity.</p>
  
  <p>Reading the documentation I used this approach:</p>
  
  <pre><code>var mainObj = _db.MyEntityA.AsNoTracking()
              .Include(e =&gt; e.MyEntityB)
              .Include(e =&gt; e.CollectionOfEntityC.Select((MyEntityC ce) =&gt; ce.MyEntityD))
  </code></pre>
  
  <p>When I run it it gives something like this:</p>
  
  <pre><code>InvalidOperationException: The Include property lambda expression 'e =&gt; {from EntityC ce in e.CollectionOfEntityC select [ce].MyEntityD}' is invalid. The expression should represent a property access: 't =&gt; t.MyProperty'. To target navigations declared on derived types, specify an explicitly typed lambda parameter of the target type, E.g. '(Derived d) =&gt; d.MyProperty'.
  </code></pre>
  
  <p>So I've tried to add the cast:</p>
  
  <pre><code>var mainObj = _db.MyEntityA.AsNoTracking()
              .Include(e =&gt; e.MyEntityB)
              .Include(e =&gt; e.CollectionOfEntityC.Select((MyEntityC ce) =&gt; ce.MyEntityD))
  </code></pre>
  
  <p>But nothing changes.</p>
  
  <p>I've tried to use .ThenInclude() this way:</p>
  
  <pre><code>    var mainObj = _db.MyEntityA.AsNoTracking()
              .Include(e =&gt; e.MyEntityB)
              .ThenInclude(e =&gt; e.Select((MyEntityC ce) =&gt; ce.MyEntityD))
  </code></pre>
  
  <p>With or without the cast, nothing changes.</p>
  
  <p>Finally, reasonably, if I remove that navigation property, it gives me this exception:</p>
  
  <pre><code>Error generated for warning 'Microsoft.EntityFrameworkCore.Infrastructure.DetachedLazyLoadingWarning: An attempt was made to lazy-load navigation property 'MyEntityD' on detached entity of type 'MyEntityCProxy'. Lazy-loading is not supported for detached entities or entities that are loaded with 'AsNoTracking()'.
  </code></pre>
  
  <p>This same scenario, worked as expected on the EF for the .Net Framework, form which I'm porting to .Net Core.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Exploring the correctly mentioned duplicates, I just wanted to add few more related aspects.</p>
  
  <p>@StriplingWarrior answer below is good; I fixed my code using .ThenInclude() instead of .Select(); now everything builds, but:</p>
  
  <ul>
  <li>IntelliSense doesn't help writing the code inside ThenInclude()</li>
  <li>if I leave <code>AsNoTraking()</code> I must disable the <code>DetachedLazyLoadingWarning</code></li>
  <li>if I remove <code>AsNoTracking()</code> EF tracks changes I don't want it to track (I want a readonly snapshot of a complex object)</li>
  </ul>