Ejemplo n.º 1
0
        private void AddContentToSelectList(List <SelectListItem> list, HierachyTree <T> node, int level,
                                            Func <T, int, string> getText, IComparer <T> comparer, object excludeRootId, int deepLitmit)
        {
            if (level > deepLitmit)
            {
                return;
            }
            object id = node.Data.GetId();

            if (Equals(id, excludeRootId))
            {
                return;
            }
            list.Add(new SelectListItem
            {
                Value = id.ToString(),
                Text  = getText(node.Data, level)
            });
            if (comparer != null)
            {
                node.OrderChildren(comparer);
            }
            foreach (HierachyTree <T> child in node.Children)
            {
                AddContentToSelectList(list, child, level + 1, getText, comparer, excludeRootId, deepLitmit);
            }
        }
Ejemplo n.º 2
0
        public static List <HierachyTree <T> > CreateForest(IEnumerable <T> source)
        {
            var dataSource = new Dictionary <object, HierachyTree <T> >();

            foreach (var item in source)
            {
                object id   = item.GetId();
                var    node = new HierachyTree <T>(item);
                dataSource[id] = node;
            }

            foreach (var item in dataSource)
            {
                HierachyTree <T> parent;
                object           parentId = item.Value.Data.GetParentId();

                if (parentId != null && dataSource.TryGetValue(parentId, out parent))
                {
                    item.Value.Parent = parent;
                    parent.Children.Add(item.Value);
                }
            }
            return(dataSource.Values.Where(i => i.Parent == null).ToList());
        }