Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6352409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:11:59+00:00 2026-05-24T22:11:59+00:00

So I have an entity that has a navigation property which has a type

  • 0

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)

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; }
}

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?

I’ve tried

ObjectContext context = GetContext();
var result = context.ParentEntities.
  Where(e => e.AnotherProperty == AnotherInterestingValue).
  Where(e => e.TheProperty is ChildSubEntity).
  Where(e => ((ChildSubEntity)e.TheProperty).
    InterestingProperty == InterestingValue).
  ToList();

And get the error “Unable to cast the type ‘ChildEntity’ to type ‘ChildSubEntity’. LINQ to Entities only supports casting Entity Data Model primitive types.”.

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?

To be clear, this is a simplification, I’m actually applying a number of conditions programmatically, building a query expression using LinqKit, with some of the conditions being on properties of ParentEntity, some on ParentEntity, and some on other child entities of ParentEntity.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:12:01+00:00Added an answer on May 24, 2026 at 10:12 pm

    You have to use OfType its correctly resolved by LINQ to Entities. Use it against ChildEntity collection and select ParentEntities related to selected ChildEntity objects.

    ObjectContext context = GetContext();
    var result = context.ChildEntities.OfType<ChildSubEntity>
    .Where(e => e.InterestingProperty == InterestingValue)
    .SelectMany(e = > e.ParentEntity)
    .ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an entity class that has a property with an underlying db column
I have a Video entity that has multiple Genres, public class Video { public
I have an object defined by my entity framework model that has navigation properties,
I have an entity that maps to an external oracle table which is one
I have a stored procedure that returns a collection of my entity class objects.
I have an entity that has few fields. One of them is city name.
If I have an entity that has an association (e.g. Book.Publisher), how do I
I would like to have a JPA Entity that has a GSon Object as
I have an Entity class that I have extended using the partial class syntax
I have entity called Customer and it has three properties: public class Customer {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.