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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:32:44+00:00 2026-05-26T05:32:44+00:00

I will start by saying this may not be conceptually correct so I will

  • 0

I will start by saying this may not be conceptually correct so I will post my problem also, so if someone can help with the underlying problem I won’t need to do this.

Here is a simplified version of my model.

public class MenuItem
{
    public int MenuItemId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Department> Departments { get; set; }

    private ICollection<MenuSecurityItem> _menuSecurityItems;
    public virtual ICollection<MenuSecurityItem> MenuSecurityItems
    {
        get { return _menuSecurityItems ?? (_menuSecurityItems = new HashSet<MenuSecurityItem>()); }
        set { _menuSecurityItems = value; }
    }
}

public class Department
{
    public int DepartmentId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<MenuItem> MenuItems { get; set; }
}

My underlying problem is that I want to select all MenuItems that belong to a Department (Department with DepartmentId = 1 for arguments sake) and also include all MenuSecurityItems.

I am unable to Include() the MenuSecurityItems as the MenuItems navigation collection is of type ICollection and doesn’t support Include(). This also doesn’t seem to work Department.MenuItems.AsQueryable().Include(m => m.MenuSecurityItems)

The way I “fixed” this issue was creating an entity for the many-to-many mapping table Code First creates.

public class DepartmentMenuItems
{
    [Key, Column(Order = 0)]
    public int Department_DepartmentId { get; set; }
    [Key, Column(Order = 1)]
    public int MenuItem_MenuItemId { get; set; }
}

I then was able to join through the mapping table like so. (MenuDB being my DBContext)

var query = from mItems in MenuDb.MenuItems
            join depmItems in MenuDb.DepartmentMenuItems on mItems.MenuItemId equals depmItems.MenuItem_MenuItemId
            join dep in MenuDb.Departments on depmItems.Department_DepartmentId equals dep.DepartmentId
            where dep.DepartmentId == 1
            select mItems;

This actually worked for that particular query… however it broke my navigation collections. Now EF4.1 is throwing an exception as it is trying to find an object called DepartmentMenuItems1 when trying to use the navigation collections.

If someone could either help me with the original issue or the issue I have now created with the mapping table entity it would be greatly appreciated.

  • 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-26T05:32:45+00:00Added an answer on May 26, 2026 at 5:32 am

    Eager loading of nested collections works by using Select on the outer collection you want to include:

    var department = context.Departments
        .Include(d => d.MenuItems.Select(m => m.MenuSecurityItems))
        .Single(d => d.DepartmentId == 1);
    

    You can also use a dotted path with the string version of Include: Include("MenuItems.MenuSecurityItems")


    Edit: To your question in comment how to apply a filter to the MenuItems collection to load:

    Unfortunately you cannot filter with eager loading within an Include. The best solution in your particular case (where you only load one single department) is to abandon eager loading and leverage explicite loading instead:

    // 1st roundtrip to DB: load department without navigation properties
    var department = context.Departments
        .Single(d => d.DepartmentId == 1);
    
    // 2nd roundtrip to DB: load filtered MenuItems including MenuSecurityItems
    context.Entry(department).Collection(d => d.MenuItems).Query()
        .Include(m => m.MenuSecurityItems)
        .Where(m => m.Active)
        .Load();
    

    This requires two roundtrips to the DB and two queries but it’s the cleanest approach in my opinion which actually only loads the data you need.

    Other workarounds are 1) either to apply the filter later in memory (but then you have to load the whole collection first from the DB before you can filter) or 2) to use a projection. This is explained here (the second and third point):

    EF 4.1 code-first: How to order navigation properties when using Include and/or Select methods?

    The examples in this answer are for ordering but the same applies to filtering (just replace OrderBy in the code snippets by Where).

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

Sidebar

Related Questions

I will start by saying that I don't think what I want can be
Ok, so I will start by saying that I am new to all this
I will start by saying this is the first time I have done anything
I will start by saying I've read this topic: C++ Return reference / stack
I'll start of by saying I'm not a developer. Yes this is a c#
Let me start by saying I know about position() but I can't seem to
Ok, lemme start out by saying Im new to this! LOL I have done
I'd like to start out saying Stack Overflow has been a huge help, just
I will start by saying that I am completely new to WPF. But, I
Let me start this off by saying that I'm an intern with no Powershell

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.