I have the following class:
public class Item
{
public Item()
{
this.Items = new List<Item>();
}
public string Id { get; set; }
public string ParentId { get; set; }
public string Name { get; set; }
public DateTime Created { get; set; }
public string Content { get; set; }
public string UserId { get; set; }
List<Item> Children { get; set; }
}
This class will provide a tree hierarchical structure with N deep levels (parent - children)
So basically when I get the hierarchy as a list:
List<Item> items = JsonConvert.DeserializeObject<List<Item>>("json in here")
The result will be similar to this: the deepness can be X levels. There can be one or more parents (ParentId = null is top level)
- Parent (Id:1, ParentId: null)
- Child (Id: 1.1, ParentId: 1)
- Child (Id: 1.1.1, ParentId: 1.1)
- Child (Id: 1.2, ParentId: 1)
- Child (Id: 1.2.1, ParentId: 1.2)
- Child (Id: 1.2.2, ParentId: 1.2)
- Child (Id: 1.3, ParentId: 1)
- Child (Id: 1.1, ParentId: 1)
- Parent (Id:2, ParentId: null)
- Child (Id: 2.1, ParentId: 2)
- Child (Id: 2.1.1, ParentId: 2.1)
- Child (Id: 2.2, ParentId: 2)
- Child (Id: 2.2.1, ParentId: 2.2)
- Child (Id: 2.2.2, ParentId: 2.2)
- Child (Id: 2.3, ParentId: 2)
- Child (Id: 2.1, ParentId: 2)
- Parent (Id:3, ParentId: null)
Now, I need to save this hierarchy into a database table.
My question how can I flatten this hierarchy to a List<Item>
?
EDIT:
The Id
and ParentId
are Guids.