I have a tree menu in my project.
The data looks like below
{ParentId = null, Id = 10, Name "a"},
{ParentId = null, Id = 33, Name "aa"},
{ParentId = 10 , Id = 11, Name "aaa"},
{ParentId = 10, Id = 12, Name "aaaa"},
{ParentId = 11, Id = 13, Name "aaaaa"},
{ParentId = 56 ,Id = 14, Name "aas"},
{ParentId = 78 , Id = 15, Name "adss"},
{ParentId = 99 , Id = 16, Name "ader"}
I have created a hierarchical list for holding the data
public class NavBarItem
{
public int? Id { get; set; }
public int? ParentId { get; set; }
public string Name{get;set;}
public IEnumerable<NavBarItem> Children { get; set; }
public int ChildCount { get; set; }
public int HierarchyLevel { get; set; }
}
And my recursive method will get data from the table and bind it to the Hierarchical List
What I am trying to get here is total number of children/grandchildren for each and every parent.
For example Parent A has child B and Child B has Child C & D, then the total ChildCount of A should be 3 , B should be 2 and C should be 0
Also I wanted to get the Hierarchy Level in each and every parent.
In the above example : Parent A has Child B and B has other child. So for parent A the hierarchyLevel is 2 and for B it should be 1 and for C it should be 0
Example if i am taking the item with Id = 10 , it has Hierarchy two (number of grand child levels )
{ParentId = 10 , Id = 11, Name "aaa"},
{ParentId = 11, Id = 13, Name "aaaaa"},
Is there any better way , or easy way I can get this ChildCount as well as the Hierarchy level.
Total Child count example :
Input is Id = 10
total childs = 3.
Current approach :
RecursiveMethod(List)
{
for each through the list and find the count
call the RecursiveMethod again
}