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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:49:38+00:00 2026-06-01T00:49:38+00:00

Possible Duplicate: linq to sql recursive query I got stuck with having to build

  • 0

Possible Duplicate:
linq to sql recursive query

I got stuck with having to build a Recursive select via LINQ for the self referencing table.

enter image description here

I use this class:

public class DivisionHierarchy
{
    public Division Division { get; set; }
    public IEnumerable<DivisionHierarchy> Divisions { get; set; }
}

and I created this function but somehow it is infinite.

public IEnumerable<DivisionHierarchy> GetDivisionHierarchy(IEnumerable<Division> allDivisions, Division parentDivision)
{
    Guid? parentDivisionId = null;

    if (parentDivision != null)
         parentDivisionId = parentDivision.DivisionID;

    var childDivisions = allDivisions.Where(e => e.DivisionID == parentDivisionId);

    Collection<DivisionHierarchy> hierarchy = new Collection<DivisionHierarchy>();

    foreach (var div in childDivisions)
       hierarchy.Add(new DivisionHierarchy() { Division = div, Divisions = GetDivisionHierarchy(allDivisions, div) });

     return hierarchy;
}

Any clue where I can start?

Thank you!

P.S. Are there any others ways to do it?


UPDATES based on http://www.scip.be/index.php?Page=ArticlesNET18#AsHierarchy

I found my errors.

There are 2 things to implement:
1. The root node should be created under the database.

enter image description here

  1. I changed code a little bit.

    Guid divisionID = Guid.Parse("5b487b3d-e9be-413f-b611-2fd7491e0d0d"); // Hardcoded somehow
    var rootDivision = db.Divisions.Where(i => i.ID == divisionID).FirstOrDefault();
    var divisionHierarchy = GetDivisionHierarchy(db.Divisions.AsEnumerable(), rootDivision);
    

    …

     public IEnumerable<DivisionHierarchy> GetDivisionHierarchy(IEnumerable<Division> allDivisions, Division parentDivision)
            {
                Guid? parentDivisionId = null;
    
                if (parentDivision != null)
                    parentDivisionId = parentDivision.ID;
    
                var childDivisions = allDivisions.Where(division => division.DivisionID == parentDivisionId);
    
                Collection<DivisionHierarchy> hierarchy = new Collection<DivisionHierarchy>();
    
                foreach (var div in childDivisions)
                {
                    DivisionHierarchy divisionHierarchy = new DivisionHierarchy();
                    divisionHierarchy.Division = div;
                    divisionHierarchy.Divisions = GetDivisionHierarchy(allDivisions, div);
                    hierarchy.Add(divisionHierarchy);
                }
    
                return hierarchy;
            }
    
  • 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-01T00:49:40+00:00Added an answer on June 1, 2026 at 12:49 am

    I would load the divisions in an non-recursive way and then set up the recursive relations in code. Here is an example, which does this in a lazy way

    public class Division
    {
        public int ID { get; set; }
        public int DivisionID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
    
        private static List<Division> _divisions;
        public static List<Division> Divisions
        {
            get
            {
                if (_divisions == null) {
                    LoadAndSetUpDivisionsHierarchy();
                }
                return _divisions;
            }
        }
    
        private static Dictionary<int, Division> _divisionsByID;
        public static Dictionary<int, Division> DivisionsByID
        {
            get
            {
                if (_divisionsByID == null) {
                    LoadAndSetUpDivisionsHierarchy();
                }
                return _divisionsByID;
            }
        }
    
        private static Division _root;
        public static Division Root
        {
            get
            {
                if (_root == null) {
                    LoadAndSetUpDivisionsHierarchy();
                }
                return _root;
            }
        }
    
        private Division _parentDivision;
        public Division ParentDivision
        {
            get
            {
                if (_parentDivision == null && DivisionID != 0) {
                    _parentDivision = DivisionsByID[DivisionID];
    
                }
                return _parentDivision;
            }
        }
    
        private List<Division> _subDivisions = new List<Division>();
        public List<Division> SubDivisions
        {
            get { return _subDivisions; }
        }
    
        private static void LoadAndSetUpDivisionsHierarchyHierarchy()
        {
            // Load the divisions in a non-recursive way using LINQ
            // (details not shown here).
            _divisions = LoadDivisions();
    
            // Add the divisions in a dictionary by id
            _divisionsByID = new Dictionary<int, Division>(_divisions.Count);
            foreach (Division division in _divisions) {
                _divisionsByID.Add(division.ID, division);
            }
    
            // Define sub-divisions and root division
            foreach (Division division in _divisions) {
                if (division.DivisionID == 0) {
                    _root = division;
                } else if (division.ParentDivision != null) {
                    division.ParentDivision.SubDivisions.Add(division);
                }
            }
        }
    
        private static List<Division> LoadDivisions()
        {
            throw new NotImplementedException();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: LINQ to SQL: Return anonymous type? I have a standard LINQ to
Possible Duplicate: How to update Linq to SQL dbml file? Is there a quick
Possible Duplicate: Linq: What is the difference between Select and Where What's the difference
Possible Duplicate: Linq help - Sql trace returns result, but datacontext returning null Question
Possible Duplicate: Equivalent of SQL ISNULL in LINQ? I am recently migrated from ADO.Net
Possible Duplicate: How to do SQL Like % in Linq? Im using mvc 3
Possible Duplicate: LINQ Between Operator Dear All, Hi, I need to write this query
Possible Duplicate: LINQ to SQL: Return anonymous type? This is my code: class B
Possible Duplicate: Query Microsoft Access MDB Database using LINQ and C# Im working with
Possible Duplicate: string1 >= string2 not implemented in Linq to SQL, any workarround? To

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.