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

  • SEARCH
  • Home
  • 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 8716963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:12:18+00:00 2026-06-13T06:12:18+00:00

There is a simple domain.. public abstract class UserComment { public string Text {

  • 0

There is a simple domain..

public abstract class UserComment
{
    public string Text { get; set; }
}

public class BlogComment : UserComment
{
    public Blog Blog { get; set; }
}

public class PhotoComment : UserComment
{
    public Photo Photo { get; set; }
}

Is there way to query all entities of type UserComment with properties Blog and Photo loaded?

var comment = DbContext.Set<UserComment>()
    .Include(x => x.Blog) // will not compile
    .Include(x => x.Photo) // will not compile
    .FirstOrDefault();

if (comment is PhotoComment )
{
    string url = (comment as PhotoComment).Photo.Url;
}
if (comment is BlogComment)
{
    var dateCreated = (comment as BlogComment).Blog.DateCreated;
}

Thanks!

  • 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-06-13T06:12:19+00:00Added an answer on June 13, 2026 at 6:12 am

    You will probably need two queries to get the result. If you only want the first element (FirstOrDefault) explicit loading – as you already proposed in the comments – is a good approach:

    var comment = DbContext.Set<UserComment>().FirstOrDefault();
    
    if (comment is BlogComment)
        DbContext.Entry(comment as BlogComment).Reference(bc => bc.Blog).Load();
    else if (comment is PhotoComment)
        DbContext.Entry(comment as PhotoComment).Reference(pc => pc.Photo).Load();
    

    If you want to load a list of UserComments that’s not the best solution as it would require to iterate over the loaded UserComments and to call explicit loading for each element which will result in many queries.

    For a list you can use the following approach that also will generate only two queries:

    IEnumerable<UserComment> blogComments = DbContext.Set<UserComment>()
        .OfType<BlogComment>()
        .Include(bc => bc.Blog)
        .Cast<UserComment>()
        .AsEnumerable();
    
    IEnumerable<UserComment> photoComments = DbContext.Set<UserComment>()
        .OfType<PhotoComment>()
        .Include(pc => pc.Photo)
        .Cast<UserComment>()
        .AsEnumerable();
    
    List<UserComment> comments = blogComments.Concat(photoComments).ToList();
    

    Because of the usage of AsEnumerable() this will run two separate database queries and concat the results to a single collection in memory.

    LINQ-to-Entities supports Cast but for some reason it is not possible to remove the two AsEnumerable() conversions to get only a single database query and concat the results in the database. The code would still compile but I had a runtime exception about an invalid Include path.

    I have tested with EF 4.1. It might be worth to test the query without AsEnumerable() with EF 5.0 to see if it still fails.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a simple domain object, Movie , with the following constructor: public Movie(string
Is there a simple way to serialize a single-level structure as a string for
Is there a simple way to get time time of day (17:30, 01:20...etc) that
I have an abstract class BaseItem declared like this: public abstract class BaseItem {
Consider the following code: public abstract class RepositoryBase<T> where T : class { #region
Take my domain class for example public class Person { private Integer id; private
A RIA Domain service has this method... public virtual CmsDealer GetCmsDealer(string id) { return
Are there simple libraries out there (.NET and Java) that are able to validate
There is simple cipher that translates number to series of . ( ) In
There is simple JSON serialization module with name simplejson which easily serializes Python objects

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.