Compose different collections with C#.
Used for creating hierarchical tree structures. A tree collection consists of two classes TreeList<T> and TreeItem<T>.
Is a list of TreeItems.
Contains the item's Data and Children.
You have factory classes to make it easier to compose trees.
var treeItem = TreeItem.New("Item")
.AddChild(TreeItem.New("Item"))
.AddChild(TreeItem.New("Item")
.AddChild(TreeItem.New("Item"))
.AddChild(TreeItem.New("Item")))
.AddChild(TreeItem.New("Item")
.AddChild(TreeItem.New("Item")
.AddChild(TreeItem.New("Item"))))
.AddChild(TreeItem.New("Item"));You can convert a flat list with a parent/child relation into a TreeList. All you need to do is to specify the Id and ParnetId property using lambda expressions.
var flatList = new List<CommentModel> {
new CommentModel { Id = "1", ParentId = null, Text = "Comment 1" },
new CommentModel { Id = "2", ParentId = null, Text = "Comment 2" },
new CommentModel { Id = "3", ParentId = null, Text = "Comment 3" },
new CommentModel { Id = "1.1", ParentId = "1", Text = "Comment 1.1" },
new CommentModel { Id = "1.2", ParentId = "1", Text = "Comment 1.2" },
new CommentModel { Id = "3.1", ParentId = "3", Text = "Comment 3.1" },
new CommentModel { Id = "3.2", ParentId = "3", Text = "Comment 3.2" },
new CommentModel { Id = "3.1.1", ParentId = "3.1", Text = "Comment 3.1.1" },
new CommentModel { Id = "3.1.2", ParentId = "3.1", Text = "Comment 3.1.2" },
};
var treeList = TreeList.Parse(flatList, i => i.Id, i => i.ParentId);For transforming the tree you can use the SelectNested(...) method.
H.List(GetComments().SelectNested<IHtmlElement>((item, childrenOutput, depth) =>
H.Div()
.AddStyle("padding-left", $"{depth + 1}0px")
.AddElement(item.Text)
.AddElements(childrenOutput)).ToArray()))Get all decendant items
H.Div().AddElements(GetComments().First().Decendants().Select(i => H.Div(i.Text))))Get it self and all it's all decendant items
H.Div().AddElements(GetComments().First().SelfAndDecendants().Select(i => H.Div(i.Text))))