title: Entity Framework - query specifying inherited type for navigation property
body: <p>So I have an entity that has a navigation property which has a type with a class hierarchy. (entity names changed to protect the guilty)</p>
  
  <pre><code>class ParentEntity
  {
    virtual ChildEntity TheProperty { get; set; }
    virtual string AnotherProperty { get; set; }
    virtual string AnotherProperty2 { get; set; }
  }
  
  class ChildEntity
  {
  }
  
  class ChildSubEntity : ChildEntity
  {
    virtual string InterestingProperty { get; set; }
  }
  </code></pre>
  
  <p>How can I query for ParentClass entities, with one of my query conditions being where TheProperty is of type ChildSubClass and InterestingProperty has a particular value?</p>
  
  <p>I've tried</p>
  
  <pre><code>ObjectContext context = GetContext();
  var result = context.ParentEntities.
    Where(e =&gt; e.AnotherProperty == AnotherInterestingValue).
    Where(e =&gt; e.TheProperty is ChildSubEntity).
    Where(e =&gt; ((ChildSubEntity)e.TheProperty).
      InterestingProperty == InterestingValue).
    ToList();
  </code></pre>
  
  <p>And get the error "Unable to cast the type 'ChildEntity' to type 'ChildSubEntity'. LINQ to Entities only supports casting Entity Data Model primitive types.".</p>
  
  <p>I'm having to settle for flattening the list, and applying this condition after the data has been retrieved from the entity store. Is it possible to write this condition in a form LINQ to Entities will accept?</p>
  
  <p>To be clear, this is a simplification, I'm actually applying a number of conditions programmatically, building a query expression using <a href="http://www.albahari.com/nutshell/linqkit.aspx" rel="noreferrer">LinqKit</a>, with some of the conditions being on properties of ParentEntity, some on ParentEntity, and some on other child entities of ParentEntity.</p>