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.

Elzo Valugi
  • 27,240
  • 15
  • 95
  • 114
devendra
  • 11
  • 1
  • 4
  • 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? – Trev Davies Apr 01 '16 at 07:26
  • @Remuze I edited it. Please check it again. Its a detail only – devendra Apr 01 '16 at 07:28
  • @Remuze The ParentID will determine the parent node. Thats how they will link together – devendra Apr 01 '16 at 07:33
  • I think you will find information here including an example: http://stackoverflow.com/questions/66893/tree-data-structure-in-c-sharp?rq=1 – Trev Davies Apr 01 '16 at 07:41
  • Could you simply use a Detail instead of an int to specify the parent? Otherwise [this](http://stackoverflow.com/questions/17592147/how-to-build-a-hierarchy-with-use-linq-to-object) can help you – Matteo Umili Apr 01 '16 at 07:44
  • Please show what you've tried and ask for specific help with programming issue(s). – MikeC Apr 10 '16 at 03:26

1 Answers1

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);
Captain0
  • 2,583
  • 2
  • 28
  • 44
  • 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 Apr 01 '16 at 08:21
  • @ironstone13 is there any better option? – devendra Apr 01 '16 at 10:45
  • @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 – ironstone13 Apr 01 '16 at 11:13