title: Entity Framework Core Include(...Where)
body: <p>According to the docs we have the possibility to use <code>Where()</code> inside <code>Include</code> in EF Core 5. So this code is working well:</p>
  <pre class="lang-cs prettyprint-override"><code>var groups = dbContext.DocumentGroups
                        .Include(e =&gt; e.Types.Where(x =&gt; true))
                        .OrderBy(e =&gt; e.Name);
  </code></pre>
  <p>Now I have to refactor my filter (in this case simplified) - so I tried an extension method like this:</p>
  <pre class="lang-cs prettyprint-override"><code>public static IEnumerable&lt;T&gt; MyFilter&lt;T&gt;(this IEnumerable&lt;T&gt; query)
  {
      // ... do a lot of filter stuff here
      return query.Where(e =&gt; true);
  }
  </code></pre>
  <p>and call it like this:</p>
  <pre class="lang-cs prettyprint-override"><code>var groups = dbContext.DocumentGroups
                        .Include(e =&gt; e.Types.MyFilter())
                        .OrderBy(e =&gt; e.Name);
  </code></pre>
  <p>I get an exception:</p>
  <blockquote>
  <p>The expression 'e.Types.MyFilter(__dbContext_1)' is invalid inside an 'Include' operation, since it does not represent a property access: 't =&gt; t.MyProperty'. To target navigations declared on derived types, use casting ('t =&gt; ((Derived)t).MyProperty') or the 'as' operator ('t =&gt; (t as Derived).MyProperty'). Collection navigation access can be filtered by composing Where, OrderBy(Descending), ThenBy(Descending), Skip or Take operations. For more information on including related data, see <a href="http://go.microsoft.com/fwlink/?LinkID=746393" rel="nofollow noreferrer">http://go.microsoft.com/fwlink/?LinkID=746393</a>.</p>
  </blockquote>
  <p>How can I fix this?</p>