1

I have a class called Detail as given below:

public class Detail
{
    public int Id { get; set; }
    public string Name { get; set; }
    public System.Nullable<int> ParentID { get; set; }
}

And I make a list of detail as given below:

        List<Detail> Details= new List<Detail>();

        Details.Add(new Detail { Id = 1, Name = "James", ParentID =null });
        Details.Add(new Detail { Id = 2, Name = "David", ParentID = 1 });
        Details.Add(new Detail { Id = 3, Name = "Richard", ParentID = 2 });
        Details.Add(new Detail { Id = 4, Name = "John", ParentID = 3 });
        Details.Add(new Detail { Id = 5, Name = "Robert", ParentID = 3 });
        Details.Add(new Detail { Id = 6, Name = "Paul", ParentID = 3 });
        Details.Add(new Detail { Id = 7, Name = "Kevin", ParentID = 2 });
        Details.Add(new Detail { Id = 8, Name = "Jason", ParentID = 7 });
        Details.Add(new Detail { Id = 9, Name = "Mark", ParentID = 7 });
        Details.Add(new Detail { Id = 10, Name = "Thomas", ParentID = 9 });
        Details.Add(new Detail { Id = 11, Name = "Donald", ParentID = 9 });

And now I want this Detail list convert into tree structure.

6
  • Please be more specific on the tree structure, how do you imagine the nodes being linked together? By this I mean, what criteria will determine which node is linked to which? Commented Apr 1, 2016 at 7:26
  • @Remuze I edited it. Please check it again. Its a detail only Commented Apr 1, 2016 at 7:28
  • @Remuze The ParentID will determine the parent node. Thats how they will link together Commented Apr 1, 2016 at 7:33
  • I think you will find information here including an example: stackoverflow.com/questions/66893/… Commented Apr 1, 2016 at 7:41
  • Could you simply use a Detail instead of an int to specify the parent? Otherwise this can help you Commented Apr 1, 2016 at 7:44

1 Answer 1

4

You can try the following

Add a new class to hold the tree object

public class TreeNode
{
  public int Id { get; set; }
  public string Name { get; set; }

  public TreeNode Parent { get; set; }
  public List<TreeNode> Children{ get; set; }
}

Then add a recursive method to build the tree

private static List<TreeNode> FillRecursive(List<Detail> flatObjects, int? parentId=null)
{
  return flatObjects.Where(x => x.ParentID.Equals(parentId)).Select(item => new TreeNode
  {
    Name = item.Name, 
    Id = item.Id, 
    Children = FillRecursive(flatObjects, item.Id)
  }).ToList();
}

Then call it where you need it

 var tree = FillRecursive(Details,null);
Sign up to request clarification or add additional context in comments.

3 Comments

I think FillRecursive does not set the TreeNode.Parent, I would say it is sufficient to have either Parent or Children property in TreeNode Also FillRecursive does not terminate on invalid input, for example {id=1,ParentId=2}, {id=2,ParentId=1}
@ironstone13 is there any better option?
@devendra - it depends on whether you need the reference to Parent node (how will you traverse the tree) and whether you expect inconsistent data in your input list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.