zoukankan      html  css  js  c++  java
  • linq递归

      public class Comment
        {
            public int Id { get; set; }
            public int ParentId { get; set; }
            public string Text { get; set; }        
            public List<Comment> Children { get; set; }
        }
    
        class Program
        {
            static void Main()
            {
            List<Comment> categories = new List<Comment>()
                {
                    new Comment () { Id = 1, Text = "Item 1", ParentId = 0},
                    new Comment() { Id = 2, Text = "Item 2", ParentId = 0 },
                    new Comment() { Id = 3, Text = "Item 3", ParentId = 0 },
                    new Comment() { Id = 4, Text = "Item 1.1", ParentId = 1 },
                    new Comment() { Id = 5, Text = "Item 3.1", ParentId = 3 },
                    new Comment() { Id = 6, Text = "Item 1.1.1", ParentId = 4 },
                    new Comment() { Id = 7, Text = "Item 2.1", ParentId = 2 }
                };
    
                List<Comment> hierarchy = new List<Comment>();
                hierarchy = categories
                                .Where(c => c.ParentId == 0)
                                .Select(c => new Comment() { Id = c.Id, Text = c.Text, ParentId = c.ParentId, Children = GetChildren(categories, c.Id) })
                                .ToList();
    
                HieararchyWalk(hierarchy);
    
                Console.ReadLine();
            }
    
            public static List<Comment> GetChildren(List<Comment> comments, int parentId)
            {
                return comments
                        .Where(c => c.ParentId == parentId)
                        .Select(c => new Comment { Id = c.Id, Text = c.Text, ParentId = c.ParentId, Children = GetChildren(comments, c.Id) })
                        .ToList();
            }
    
            public static void HieararchyWalk(List<Comment> hierarchy)
            {
                if (hierarchy != null)
                {
                    foreach (var item in hierarchy)
                    {
                        Console.WriteLine(string.Format("{0} {1}", item.Id, item.Text));
                        HieararchyWalk(item.Children);
                    }
                }
            }
  • 相关阅读:
    03:信号与槽,以字体修改为例
    赞不绝口点赞器原作者的撤项信
    钉钉魔铃 铃声切换器 1.0 项目失败
    KC开发组官方网站
    搜索引擎知识
    现在的手机厂家真浮躁
    02:QT的第一个程序
    第一次做编程语言的英语阅读理解
    mysql 报错[Err] [Dtf] 1292
    Mysql 精确查询是否字段中包含某个字符串
  • 原文地址:https://www.cnblogs.com/-Apple/p/4487366.html
Copyright © 2011-2022 走看看