Mad.Collections

Compose different collections with C#.


Tree Collection

Used for creating hierarchical tree structures. A tree collection consists of two classes TreeList<T> and TreeItem<T>.


TreeList

Is a list of TreeItems.


TreeItem

Contains the item's Data and Children.


Compose a Tree

You have factory classes to make it easier to compose trees.

Item
Item
Item
Item
Item
Item
Item
Item
Item
            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"));

Parse to a Tree

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.

Comment 1
Comment 1.1
Comment 1.2
Comment 2
Comment 3
Comment 3.1
Comment 3.1.1
Comment 3.1.2
Comment 3.2
        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);

Select Nested

For transforming the tree you can use the SelectNested(...) method.

Comment 1
Comment 1.1
Comment 1.2
Comment 2
Comment 3
Comment 3.1
Comment 3.1.1
Comment 3.1.2
Comment 3.2
        H.List(GetComments().SelectNested<IHtmlElement>((item, childrenOutput, depth) =>
            H.Div()
                .AddStyle("padding-left", $"{depth + 1}0px")
                .AddElement(item.Text)
                .AddElements(childrenOutput)).ToArray()))

Decendants

Get all decendant items

Comment 1.1
Comment 1.2
        H.Div().AddElements(GetComments().First().Decendants().Select(i => H.Div(i.Text))))

Self and Decendants

Get it self and all it's all decendant items

Comment 1
Comment 1.1
Comment 1.2
        H.Div().AddElements(GetComments().First().SelfAndDecendants().Select(i => H.Div(i.Text))))