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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:54:17+00:00 2026-05-26T06:54:17+00:00

I can’t seem to get my head around this. I need to build some

  • 0

I can’t seem to get my head around this. I need to build some test data to build a tree menu, but I either get a stackverflow error because it just keeps going or I only get the first level of children. Here is the code I currently have:

private MenuItem _menuItem;
private int _start = 1;
private const int Stop = 5;

public void BuildMenu()
{
    var numberOfChildren = new Random((int) DateTime.Now.Ticks).Next(3, 40);
    AutoMapper.Mapper.CreateMap<MenuItem, MenuItemDto>().ConvertUsing<MenuItemConverter>();
    _menuItem = new MenuItem() {Id = Guid.NewGuid(), Name = "Main Menu"};
    _menuItem.ChildMenuItems = BuildChildItems(_menuItem, numberOfChildren);

    var menuDto = AutoMapper.Mapper.Map<MenuItem, MenuItemDto>(_menuItem);
}

public ICollection<MenuItem> BuildChildItems(MenuItem parentMenuItem, int numberOfChildren)
{
    var childItems = new Collection<MenuItem>();

    _start += 1;

    for (var i = 0; i <= numberOfChildren; i++)
    {
        var childItem = new MenuItem()
                            {
                                Id = Guid.NewGuid(),
                                ParentItemId = parentMenuItem.Id,
                                Parentitem = parentMenuItem,
                                Name = "Child Menu Item  " + DateTime.Now
                            };

        if (_start != Stop)
        {
            childItem.ChildMenuItems = BuildChildItems(childItem, numberOfChildren);
        }

        childItems.Add(childItem);
    }

    return childItems;
}

public class MenuItem
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public Guid? ParentItemId { get; set; }
    public MenuItem Parentitem { get; set; }
    public ICollection<MenuItem> ChildMenuItems { get; set; }
}

While this works, you will notice the _start and Stop variables. I put those in so I don;t get a stackoverflow, but obviously that is why I only get the 1st level of each with populated children. I figure I would need to track what level I am in, but not sure how to keep track of it. Not sure why I am drawing a blank today….

  • 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-26T06:54:17+00:00Added an answer on May 26, 2026 at 6:54 am

    This gives the idea rather than actually looking at your specific object model! Have an int which represents the number of levels you want to go to. Send this into the method, decrement it on each recurse until it is zero.

    public void BuildChildItems(MenuItem parentMenuItem, int numberOfChildren, int level)
    {
      if(level == 0) return;
    
      var results_for_this_level = create_results_for_this_level();
      parentMenuItem.Add(results_for_this_level);
    
    
      foreach(sibling in results_for_this_level)
      {
         BuildChildItems(sibling, numberofChildren, level-1) //note the decrement of level here
      } 
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can find why i get this error can someone help? package Android.data; public class
Can I change the field public virtual ClassOne ClassOne { get; set; } to
Can any one tell, how to get the result of LINQ query contains group
Can anyone explain to me why this program: for(float i = -1; i <
Can I run this in a Windows command prompt like I can run it
Can someone maybe tell me why this is not working? I have used echo
Can anybody tell me a regular expression to use within some PHP to find
Can I have a project that has some parts written in c and other
Can some one confirm me that only one UIWindow instance is possible in any
Can MOSS integrate and get user profiles from multiple Active Directory and/or LDAP stores?

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.