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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:09:41+00:00 2026-05-25T01:09:41+00:00

I have the following table structure: Table A, Table B, Table C and Table

  • 0

I have the following table structure:

Table A, Table B, Table C and Table D where

A 1..* B
A 1..* C
C 1..* D

To begin with I wanted all data returned (via a WCF service so no lazy loading) so this was the original code:

var a = (from a in context.As.Include("Bs").Include("Cs.Ds")
         where a.Id = paramId
         select a).SingleOrDefault();

This returns an A object with EntityCollections of Bs and Cs and Cs each had an EntityCollection of Ds

However, both B and C have an IsActive flag and I now want to only return Bs and Cs where Active.

I know that I now cannot use .Include as this will always return the entire collection.

After some googling, so far I have:

var query = (from a in context.As
            where a.Id = paramId
            select new
                {
                    A = a, 
                    Bs = from b in a.Bs where b.IsActive select b, 
                    Cs = from c in a.Cs where c.IsActive select c
                }).SingleOrDefault();

Is this the best way to form the query?

I then transform the anonymous type into an A object:

var a = query.A;

foreach (var b in query.Bs)
{
    a.Bs.Add(b);
}

foreach (var c in query.Cs)
{
    a.Cs.Add(c);
}

Is this the best way to combine the As, Bs and Cs into an A with EntityCollections?

How do I also get the Ds as a collection for every C?

  • 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-25T01:09:42+00:00Added an answer on May 25, 2026 at 1:09 am

    As far as I know, you can’t filter out the included enities: once you write the include, you get all the connected entities.

    One of the approaches is to select the connected entities in different collections and track them separately.

    On the other hand, you could include the connected collections and apply filtering when you use them.

    Please note that your current approach has a drawback:
    Your newly created entities are disconnected from context. Which means you’re disabling the self-tracking capabilities when you use them.

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

Sidebar

Related Questions

I have already googled for this I have a Table with following structure in
I have the following table structure: I'm trying to populate a combobox of all
I have the following table structure with data as ReadingDate Unit 01-05-2011 10 01-06-2011
I have the following table structure. I just want to update SubId to all
I have the following table structure with live data in it: CREATE TABLE IF
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I have the following table structure CREATE TABLE `table` ( `id` int(11) NOT NULL
I have the following table structure (in MySQL): DocID, Code, IsDup, DopOf , where
I have the following table structure. A | B | C I wish to
I have the following table structure, which is imported into an Entity Framework project:

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.