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
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.
A class to represent each item of data:
A function to get the children of a specific item:
A function to create an item including the child collection. This function will recurse down the hierarchy:
A function to get rows of the top-level items:
Putting it all together: