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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:04:43+00:00 2026-06-03T03:04:43+00:00

I want to create a class structure that can represent the following heirarchichal structure

  • 0

I want to create a class structure that can represent the following heirarchichal structure of objects all of the same type

-Parent1
- - Child1 
- - - ChildA of Child1
- - - ChildB of Child1
- - Child2 
- - - ChildA of Child2
- - - ChildB of Child2
- Parent2

The datatable rows have an ID, ParentID, name and level

level is O for Parent, 1 for Child 1 , 2 for ChildA and so on

I’m able to return the data from the DB to a datatable but am struggling after that
any help on creating the class structure and then populating the object would be greatly appreciated

  • 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-03T03:04:44+00:00Added an answer on June 3, 2026 at 3:04 am

    Here is an example on how to do that using LINQ. First a sampe datatable. I assume that top-level items have a parent ID of 0.

    var dataTable = new DataTable();
    dataTable.Columns.Add("Id", typeof(Int32));
    dataTable.Columns.Add("ParentId", typeof(Int32));
    dataTable.Columns.Add("Name", typeof(String));
    dataTable.Rows.Add(new Object[] { 1, 0, "A" });
    dataTable.Rows.Add(new Object[] { 2, 1, "B" });
    dataTable.Rows.Add(new Object[] { 3, 1, "C" });
    dataTable.Rows.Add(new Object[] { 4, 0, "D" });
    dataTable.Rows.Add(new Object[] { 5, 4, "E" });
    

    A class to represent each item of data:

    class Item {
    
      public Int32 Id { get; set; }
    
      public String Name { get; set; }
    
      public IEnumerable<Item> Children { get; set; }
    
    }
    

    A function to get the children of a specific item:

    IEnumerable<DataRow> GetChildren(DataTable dataTable, Int32 parentId) {
      return dataTable
        .Rows
        .Cast<DataRow>()
        .Where(row => row.Field<Int32>("ParentId") == parentId);
    }
    

    A function to create an item including the child collection. This function will recurse down the hierarchy:

    Item CreateItem(DataTable dataTable, DataRow row) {
      var id = row.Field<Int32>("Id");
      var name = row.Field<String>("Name");
      var children = GetChildren(dataTable, id)
        .Select(r => CreateItem(dataTable, r))
        .ToList();
      return new Item { Id = id, Name = name, Children = children };
    }
    

    A function to get rows of the top-level items:

    IEnumerable<DataRow> GetTopLevelRows(DataTable dataTable) {
      return dataTable
        .Rows
        .Cast<DataRow>()
        .Where(row => row.Field<Int32>("ParentId") == 0);
    }
    

    Putting it all together:

    var items = GetTopLevelRows(dataTable)
      .Select(row => CreateItem(dataTable, row))
      .ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a C++ class with the following type: It can be
I want to create a class that will mainly house a Vector . The
I want to create a class that initializes a timer which will be used
I want to create whois class like that public class DomainInfo { public string
Let's say that I want a class that represents a data structure in memory.
I have the following structure: class FeatureType(models.Model): type = models.CharField(max_length=20) def __unicode__(self): return self.type
I want to create a utility class that loads images contained in the assets/
Having some trouble understanding variable scope within a class structure. I want to create
Currently I have a class called user that I want to create with different
I want to create a class with a nested enum. public class Foo {

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.