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 8269553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:16:20+00:00 2026-06-08T06:16:20+00:00

I’m wanting to retrieve a set of objects ( A ) and a number

  • 0

I’m wanting to retrieve a set of objects (A) and a number of related objects (B) within a single query. There are no navigation properties on A and B so I believe I need to use this syntax:

select a from db.As where a.Id = x select new AndBHolder
{
   A = a,
   Bs = select b from db.Bs where b.ASomeId == A.SomeId select b
}

I’m not sure if this is the best way, but it does work. However I now need to include a property of B (a list of Cs). I wasn’t expecting it to work but I tried:

select a from db.As where a.Id = x select new AndBHolder
{
   A = a,
   Bs = select b from db.Bs.Include("Cs") where b.ASomeId == A.SomeId select b
}

Which fails with “Method … Include declared on type … cannot be called with instance of type …”. Next I thought I’d try a projection within a projection:

select a from db.As where a.Id = x select new AndBHolder
{
   A = a,
   Bs = select b from db.Bs where b.ASomeId == A.SomeId select new B
        {
           Id = b.Id,
           // ...
           Cs = select c from db.Cs where c.BId == b.Id select c
        }
}

Which fails with “The entity or complex type ‘B‘ cannot be constructed in a LINQ to Entities query” because you are not allowed to project into a mapped object. So how do I retrieve the Bs with their Cs populated, or is the secondary level inclusion just not supportted? Am I just going to have to make two calls to the database, one to retrieve the As and one to retrieve the Bs with their Cs?

  • 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-08T06:16:23+00:00Added an answer on June 8, 2026 at 6:16 am

    If you use a projection with select new you can’t use Include anymore, it will be ignored. Instead you must add the related entities to the projected data, for example:

    var query = from a in db.As
                join b in db.Bs on a.SomeId equals b.ASomeId into bGroup
                where a.Id = x
                select new AndBHolder
                {
                    A = a,
                    Bs = bGroup, // Bs must be of type IEnumerable<B>
                    Cs = bGroup.SelectMany(bg => bg.Cs) // Cs is IEnumerable<C>
                };
    

    If the relationship between B and C is one-to-many (not many-to-many) EF should populate the Cs collection in every loaded B automatically (that’s called “relationship fixup”) which means that you could throw away the Cs from the projection after the data have be loaded from the database. To put this together you can try to write the query like so:

    var query = (from a in db.As
                 join b in db.Bs on a.SomeId equals b.ASomeId into bGroup
                 where a.Id = x
                 select new
                 {
                     A = a,
                     Bs = bGroup,
                     Cs = bGroup.SelectMany(bg => bg.Cs)
                 })
                 .AsEnumerable() // DB query gets executed here
                 .Select(y => new AndBHolder
                 {
                     A = y.A,
                     Bs = y.Bs // the Cs in every B should be populated here
                 });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have thousands of HTML files to process using Groovy/Java and I need to
I'm trying to create an if statement in PHP that prevents a single post
I know there's a lot of other questions out there that deal with this

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.