Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can write the filter in the <code>Where</code> clause the following way:</p> <pre><code>.Where(t =&gt; t.Content is Image &amp;&amp; !(t.Content as Image).Private) </code></pre> <p>However, the bigger problem is the <code>Include</code> part. The <code>Author</code> property only exists for the derived type <code>Image</code> but <code>Include</code> will try to load the base type <code>Content</code> (which doesn't have an <code>Author</code> property) because that's the type of the navigation property <code>Content</code> in <code>Tag</code>. You just can't use <code>Include</code> here.</p> <p>You can try to rewrite the query as a projection:</p> <pre><code>var tags = context.Tags .Where(t =&gt; t.Content is Image &amp;&amp; !(t.Content as Image).Private) .Select(t =&gt; new { Tag = t, Image = t.Content as Image, // possibly this line is not needed Author = (t.Content as Image).Author, Person = t.Person }) .AsEnumerable() .Select(x =&gt; x.Tag) .ToList(); </code></pre> <p>As long as you don't disable change tracking (with <code>AsNoTracking</code> for example) EF should put the object graph together automatically so that the loaded tags have a populated <code>Content</code>, <code>Content.Author</code> and <code>Person</code> property (<em>as if</em> you had loaded the navigation properties with <code>Include</code>).</p> <p>BTW: The feature to include navigation properties of a derived type is requested <a href="http://data.uservoice.com/forums/72025-entity-framework-feature-suggestions/suggestions/1249289-include-property-of-derived-classes" rel="noreferrer">here on UserVoice</a>. It's not exactly the same as your situation, but in the comment section is a request even for your scenario.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload