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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:01:12+00:00 2026-05-17T16:01:12+00:00

I have a database table that holds parent and child records much like a

  • 0

I have a database table that holds parent and child records much like a Categories table. The ParentID field of this table holds the ID of that record’s parent record…

My table columns are: SectionID, Title, Number, ParentID, Active

I only plan to allow my parent to child relationship go two levels deep. So I have a section and a sub section and that it.

I need to output this data into my MVC view page in an outline fashion like so…

  1. Section 1
    • Sub-Section 1 of 1
    • Sub-Section 2 of 1
    • Sub-Section 3 of 1
  2. Section 2
    • Sub-Section 1 of 2
    • Sub-Section 2 of 2
    • Sub-Section 3 of 2
  3. Section 3

I am using Entity Framework 4.0 and MVC 2.0 and have never tried something like this with LINQ. I have a FK set up on the section table mapping the ParentID back to the SectionID hoping EF would create a complex “Section” type with the Sub-Sections as a property of type list of Sections but maybe I did not set things up correctly.

So I am guessing I can still get the end result using a LINQ query. Can someone point me to some sample code that could provide a solution or possibly a hint in the right direction?

alt text

Update:

I was able to straighten out my EDMX so that I can get the sub-sections for each section as a property of type list, but now I realize I need to sort the related entities.

var sections = from section in dataContext.Sections
                       where section.Active == true && section.ParentID == 0
                       orderby section.Number
                       select new Section
                       { 
                        SectionID = section.SectionID,
                        Title = section.Title,
                        Number = section.Number,
                        ParentID = section.ParentID,
                        Timestamp = section.Timestamp,
                        Active = section.Active,
                        Children = section.Children.OrderBy(c => c.Number)
                       };

produces the following error.
Cannot implicitly convert type ‘System.Linq.IOrderedEnumerable’ to ‘System.Data.Objects.DataClasses.EntityCollection’

  • 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-17T16:01:12+00:00Added an answer on May 17, 2026 at 4:01 pm

    Your model has two navigation properties Sections1 and Section1. Rename the first one to Children and the second one to Parent.

    Depending on whether you have a root Section or perhaps have each top-level section parented to itself (or instead make parent nullable?), your query might look something like:-

      // assume top sections are ones where parent == self
      var topSections = context.Sections.Where(section => section.ParentId == SectionId);
    
      // now put them in order (might have multiple orderings depending on input, pick one)
      topSections = topSections.OrderBy(section => section.Title);
    
      // now get the children in order using an anonymous type for the projection
      var result = topSections.Select(section => new {top = section, children = section.Children.OrderBy(child => child.Title)});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm really upset with Hibernate! I have a database table (mysql) that holds parent-child
I have a database that contains a table that looks a bit like this:
I have a MySql database that holds datetime in one field, in this format:
I have a large database and would like to select table names that have
I have a table with two child tables. For each record in the parent
I have a database table that holds the following grade information for a student:
I have a database table that holds information for received files. One of the
I happen to have a database with a table that holds all possible combination
I have a database table that holds order information and saves the current gold
I have a table that holds stats for a game, this table will have

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.