Here's a class I made:
public class ItemTree
{
public Int32 id { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public String text { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public List<ItemTree> item { get; set; }
public int parentId { get; set; }
}
And here's how I use it:
var tree = new ItemTree();
tree.id = 0;
tree.text = "sometext";
tree.item = new List<ItemTree>();
foreach (...)
{
if (tree.item.Count == 0)
{
tree.item.Add(new ItemTree
{
id = my_id,
text = my_name,
item = new List<ItemTree>(),
parentId = my_par
});
}
else
{
tree.item.Where(x => x.id == my_par)
.Select(x => x.item)
.First()
.Add(new ItemTree
{
id = my_id,
text = my_name,
item = new List<ItemTree>(),
parentId = my_par
});
}
}
And it crashes in the line with the Where clause. the reason it crashes is this: the tree has one item who has a list of items, and my query only checks the first item of the tree, not his children.
How to search in the whole depth of the tree and add an item there?