/// <summary> /// Recursive method to build a tree. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="items"></param> /// <param name="allItems"></param> /// <param name="nodes"></param> /// <param name="childSelector"></param> private static void AddItemsToTree <T>(IEnumerable <T> items, IEnumerable <T> allItems, TreeNodes <T> nodes, Func <T, T, bool> childSelector) { foreach (var item in items) { var childNode = new TreeNode <T>(item); AddItemsToTree <T>(allItems.Where(i => childSelector(item, i)), allItems, childNode.Nodes, childSelector); nodes.Add(childNode); } }
private static void TransformNodes <TIn, TOut>(TreeNodes <TIn> inNodes, TreeNodes <TOut> outNodes, Func <TIn, TOut> transform) { foreach (var inNode in inNodes) { var transFormedItem = transform(inNode.Item); var outNode = new TreeNode <TOut>(transFormedItem); outNodes.Add(outNode); TransformNodes(inNode.Nodes, outNode.Nodes, transform); } }
private static void AddItemsToTreeFromHierarchy <T>(TreeNodes <T> nodes, IEnumerable <T> items, Func <T, IEnumerable <T> > childSelector) { foreach (var item in items) { var node = new TreeNode <T>(item); nodes.Add(node); var childItems = childSelector(item); AddItemsToTreeFromHierarchy <T>(node.Nodes, childItems, childSelector); } }