title: Filtering with EF Core 2.1 inheritance
body: <p>I'm trying to find a way to filter my results in EF Core 2.1, when using inherited objects. </p>
  
  <p>I've got a base model and <em>several</em> inherited classes (but I've just included one): </p>
  
  <pre><code>public class Like {
      public int Id { get; set; }
      public LikeType LikeType { get; set; }
  }
  
  public class DocumentLike : Like {
      [ForeignKey(nameof(Document))]
      public int DocumentId { get; set; }
      public virtual Document Document { get; set; }
  }
  </code></pre>
  
  <p><code>LikeType</code> is an enum which is defined as the discriminator in the dbcontext. Every <code>Document</code> has a boolean property <code>.IsCurrent</code>. </p>
  
  <p>To get all items from the database, I'm using a query like:</p>
  
  <pre><code>IQueryable&lt;Like&gt; query = _context.Set&lt;Like&gt;()
      .Include(x =&gt; x.Owner)
      .Include(x =&gt; (x as DocumentLike).Document.DocumentType)
      .Include(x =&gt; (x as ProductLike).Product)
      .Include(x =&gt; (x as TrainingLike).Training)
  </code></pre>
  
  <p>This works beautifully, and returns all objects with the included sub-objects without any error. What I'm trying to do, is to get all items from the database for which the linked document has <code>.IsCurrent == true</code>. I've tried adding the following to the query above, but both result in an exception:</p>
  
  <pre><code>.Where(x =&gt; (x as DocumentLike).Document.IsCurrent == true)
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>.Where(x =&gt; x.LikeType == LikeType.Document ? (x as DocumentLike).Document.IsCurrent == true : true) 
  </code></pre>
  
  <p>The exception, which is thrown when I'm executing the query:</p>
  
  <pre><code>NullReferenceException: Object reference not set to an instance of an object.
      lambda_method(Closure , TransparentIdentifier&lt;TransparentIdentifier&lt;TransparentIdentifier&lt;TransparentIdentifier&lt;TransparentIdentifier&lt;TransparentIdentifier&lt;TransparentIdentifier&lt;TransparentIdentifier&lt;TransparentIdentifier&lt;TransparentIdentifier&lt;TransparentIdentifier&lt;TransparentIdentifier&lt;Like, ApplicationUser&gt;, Organisation&gt;, Training&gt;, Product&gt;, Platform&gt;, NewsItem&gt;, Event&gt;, Document&gt;, DocumentType&gt;, Course&gt;, CourseType&gt;, ApplicationUser&gt; )
      System.Linq.Utilities+&lt;&gt;c__DisplayClass1_0&lt;TSource&gt;.&lt;CombinePredicates&gt;b__0(TSource x)
      System.Linq.Enumerable+WhereSelectEnumerableIterator&lt;TSource, TResult&gt;.MoveNext()
      Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider._TrackEntities&lt;TOut, TIn&gt;(IEnumerable&lt;TOut&gt; results, QueryContext queryContext, IList&lt;EntityTrackingInfo&gt; entityTrackingInfos, IList&lt;Func&lt;TIn, object&gt;&gt; entityAccessors)+MoveNext()
      Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider+ExceptionInterceptor&lt;T&gt;+EnumeratorExceptionInterceptor.MoveNext()
      System.Collections.Generic.List&lt;T&gt;.AddEnumerable(IEnumerable&lt;T&gt; enumerable)
      System.Linq.Enumerable.ToList&lt;TSource&gt;(IEnumerable&lt;TSource&gt; source)
  </code></pre>
  
  <p>Is there a way to do this?</p>
  
  <p><strong>UPDATE:</strong>
  To clarify: I'm looking to get a single query that returns all <code>Like</code>-objects from the database, regardless of their (sub)types. In case the subtype is <code>DocumentLike</code>, I only want the objects that are linked to a document that has <code>.IsCurrent == true</code>.</p>