I have a list with nth level of children object. I want to traverse through the list and get required data to another list having different structure using Linq.
public class Node
{
public List<Node> Children = new List<Node>();
public Node Parent { get; set; }
public FolderReportItem AssociatedObject { get; set; }
}
I have list of IEnumerable which contains data.
list of nodes with child up to nth level
I am using Linq to create a new object with linq data.
Here is code how I am creating new object
var jsonTree = new List<object>();
foreach (var node in nodesList)
{
jsonTree.Add(new
{
id = node.AssociatedObject.ID,
name = node.AssociatedObject.Name,
children = node.Children.Select(p => new
{
id = p.AssociatedObject.ID,
name = p.AssociatedObject.Name,
children = p.Children.Select(q => new
{
id = q.AssociatedObject.ID,
name = q.AssociatedObject.Name
})
})
});
}
It is not giving me data to nth level as it missing recursive method to read data. How to transfer this to recursive method or is there other way to do this.