C# 获取多重组织机构树形节点

整理生成树形结构的方法,非原创

1、定义测试类,实际使用可以从数据库获取

public class Category
    {
        public int CategoryId { get; set; }
        public string Name { get; set; }
        public int? ParentCategoryId { get; set; }
    }

2、定义树形节点类

public class TreeItem<T>
    {
        public T Item { get; set; }
        public IEnumerable<TreeItem<T>> Children { get; set; }
    }

3、定义扩展方法 返回树形结构

  public static class Help
    {
        public static IEnumerable<TreeItem<T>> GenerateTree<T, K>(
        this IEnumerable<T> collection,
        Func<T, K> idSelector,
        Func<T, K> parentIdSelector,
        K rootId = default(K))
        {
            foreach (var c in collection.Where(u =>
            {
                var selector = parentIdSelector(u);
                return (rootId == null && selector == null)
                || (rootId != null && rootId.Equals(selector));
            }))
            {
                System.Threading.Thread.Sleep(3000);
                yield return new TreeItem<T>
                {
                    Item = c,
                    Children = collection.GenerateTree(idSelector, parentIdSelector, idSelector(c))
                };
            }
        }
       
    }

4、测试验证

private void button1_Click(object sender, EventArgs e)
        {
            var categories = new List<Category>
            {
                new Category { CategoryId = 1, Name = "Electronics", ParentCategoryId = null },
                new Category { CategoryId = 2, Name = "Mobile Phones", ParentCategoryId = 1 },
                new Category { CategoryId = 3, Name = "Laptops", ParentCategoryId = 1 },
                new Category { CategoryId = 4, Name = "Smartphones", ParentCategoryId = 2 },
                new Category { CategoryId = 5, Name = "Gaming Laptops", ParentCategoryId = 3 },
                new Category { CategoryId = 6, Name = "Clothing", ParentCategoryId = null },
                new Category { CategoryId = 7, Name = "Men's Clothing", ParentCategoryId = 6 },
                new Category { CategoryId = 8, Name = "Women's Clothing", ParentCategoryId = 6 },
            };
            var tree = categories.GenerateTree(
           c => c.CategoryId,      // idSelector
           c => c.ParentCategoryId  // parentIdSelector
            );
            PrintTree(tree, 0);
        }

      private  void PrintTree<T>(IEnumerable<TreeItem<T>> tree, int indent)
        {
            foreach (var node in tree)
            {
                if (node.Item is Category)
                {
                    Category category = node.Item as Category;
                    string msg = string.Format("{0}{1}", new string(' ', indent * 2), category.Name);
                    Console.WriteLine(msg);
                }
                PrintTree(node.Children, indent + 1);
            }
        }

5、验证结果

Electronics
  Mobile Phones
    Smartphones
  Laptops
    Gaming Laptops
Clothing
  Men's Clothing
  Women's Clothing
  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值