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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:14:38+00:00 2026-05-26T04:14:38+00:00

I am using Entity Framework 4.1 code first . Here is my Category class:

  • 0

I am using Entity Framework 4.1 code first.

Here is my Category class:

public class Category
{
     public int Id { get; set; }
     public string Name { get; set; }
     public bool IsActive { get; set; }
     public int? ParentCategoryId { get; set; }
     public virtual Category ParentCategory { get; set; }
     public virtual ICollection<Category> ChildCategories { get; set; }
}

The above class is a self-referencing category, for example, a parent category can have a list of child categories.

I want to create a string value of the parent category name and the child category name, for example, Parent Category 1 > Child Category 1-1.

So I get a list of all the parent categories, loop through each parent category. And for each parent category I want to loop through the child category list and combine each child category’s name to the parent category’s name, so that I have something like:

Animal > Lion
Anumal > Baboon
Anumal > Zebra
etc etc etc...

Here is my looping code. If anyone can help me reduce the lines of code then I would appreciate it 🙂

public IEnumerable<Category> GetParentChildCategories()
{
     IEnumerable<Category> parentCategoryList = GetParentCategories()
          .Where(x => x.IsActive);
     List<Category> parentChildCategoryList = new List<Category>();

     foreach (Category parentCategory in parentCategoryList)
     {
          foreach (Category childCategory in parentCategory.ChildCategories)
          {
               if (childCategory.IsActive)
               {
                    Category category = new Category
                    {
                         Id = childCategory.Id,
                         Name = parentCategory.Name + " > " + childCategory.Name
                    };
                    parentChildCategoryList.Add(category);
               }
          }
     }

     return parentChildCategoryList;
}

It bombs out in the 2nd foreach when wanting to loop through the child categories. Why is is this? Here is the error:

There is already an open DataReader associated with this Command which must be closed first.

  • 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-26T04:14:39+00:00Added an answer on May 26, 2026 at 4:14 am

    EF opens a reader when you iterate parentCategoryList. Then again when you try to iterate parentCategory.ChildCategories EF will open a Reader. Since there is open reader it will throw an error.

    What you should do is eager load the ChildCategories. This way EF does not have to open a reader again.

    So inside your GetParentCategories() method, use Include to eager load them

    return db.Categories.Include(c => c.ChildCategories).Where(/* */);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using the CTP 5 of Entity Framework code-first library (as announced here )
I'm using SqlServerCompact, Entity framework with code-first and I'm generating the connection string dinamically
How to declare a one to one relationship using Entity Framework 4 Code First
Is there a way in Entity Framework 4 (using CTP4 and Code First if
I am using Entity Framework 4.1 code first and ASP.NET MVC 3 and I
Using the Entity Framework Code First paradigm I have defined the following objects and
I am using Entity framework Code First CTP5 and I am trying to find
I'm working on an MVC site with Entity Framework Code First, using Ninject for
Using Entity Framework Code First CTP5, how do I create a primary key column
I'm using Entity Framework 4 Code First in my .net MVC 2.0 project and

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.