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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:51:06+00:00 2026-06-09T10:51:06+00:00

Given the following class, used to build a tree hierarchy: public class simpletest {

  • 0

Given the following class, used to build a tree hierarchy:

public class simpletest
{
    public simpletest Parent { get; set; }
    public IList<simpletest> Children { get; set; }
    public string Name { get; set; }
}

How could I get a list of all objects for a specific level?

Here is some sample data (hopefully its formatted correctly):

var root = new simpletest() { Name = "Root" };
var level1childA = new simpletest() { Name = "level1childA", Parent = root };
var level1childB = new simpletest() { Name = "level1childB", Parent = root };

var level2childA = new simpletest() { Name = "level2childA", Parent = level1childA };
var level2childB = new simpletest() { Name = "level2childB", Parent = level1childA };
var level2childC = new simpletest() { Name = "level2childC", Parent = level1childB };
var level2childD = new simpletest() { Name = "level3childD", Parent = level1childB };

var level3childA = new simpletest() { Name = "level3childA", Parent = level2childA };
var level3childB = new simpletest() { Name = "level3childB", Parent = level2childA };
var level3childC = new simpletest() { Name = "level3childC", Parent = level2childB };
var level3childD = new simpletest() { Name = "level3childD", Parent = level2childB };
var level3childE = new simpletest() { Name = "level3childE", Parent = level2childC };
var level3childF = new simpletest() { Name = "level3childF", Parent = level2childC };
var level3childG = new simpletest() { Name = "level3childG", Parent = level3childD };
var level3childH = new simpletest() { Name = "level3childH", Parent = level3childD };

level2childA.Children = new List<simpletest> { level3childA, level3childB };
level2childB.Children = new List<simpletest> { level3childC, level3childD };
level2childC.Children = new List<simpletest> { level3childE, level3childF };
level2childD.Children = new List<simpletest> { level3childG, level3childH };

level1childA.Children = new List<simpletest> { level2childA, level2childB };
level1childB.Children = new List<simpletest> { level2childC, level2childD };

So for example, using node level3childE, how would I get all the nodes from the same level? The node names can be anything.

  • 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-09T10:51:07+00:00Added an answer on June 9, 2026 at 10:51 am

    Here’s an (untested) recursive approach implemented as a member function of simpletest:

    IEnumerable<simpletest> ElementsAtDepth(int depth) {
        if(depth > 0) {
            foreach(simpletest child in this.Children)
                foreach(simpletest element in child.ElementsAtDepth(depth - 1))
                    yield return element;
        }
        else {
            foreach(simpletest element in this.Children)
                yield return element;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following object: public class Product { string Name {get;} int Quantity {get;}
Given the following code : public class Game { private Map<String,Coordinate> m_myHashMap; ... //
I have the following: class Parent { public: virtual bool foo(vector<string> arg1, vector<string> arg2)
Given the following class: class A { public List<B> ListB; // etc... } where
Given the following multiton: public class Multiton { private static final Multiton[] instances =
Given the following types private class MyTestDummyClassValidationDef : ValidationDef<MyTestDummyClass> { ... } public class
Given the following code: public class A { static final long tooth = 1L;
Given the following two classes: public class ABC { public void Accept(Ordering<User> xyz) {
Rails 3.0.4 Given the following relationship class Child < ActiveRecord::Base belongs_to :parent end class
Given the following code: class foo { }; class bar: public foo { public:

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.