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 = , Text = "Item 1", ParentId = },
new Comment() { Id = , Text = "Item 2", ParentId = },
new Comment() { Id = , Text = "Item 3", ParentId = },
new Comment() { Id = , Text = "Item 1.1", ParentId = },
new Comment() { Id = , Text = "Item 3.1", ParentId = },
new Comment() { Id = , Text = "Item 1.1.1", ParentId = },
new Comment() { Id = , Text = "Item 2.1", ParentId = }
}; List<Comment> hierarchy = new List<Comment>();
hierarchy = categories
.Where(c => c.ParentId == )
.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);
}
}
}

linq递归的更多相关文章

  1. 【Linq递归查找系列】

    Linq递归查找: public IEnumerable<MenuInfo> GetTree(int id, IEnumerable<MenuInfo> lst) { var ...

  2. LinQ To Object 基本用法

    http://www.cnblogs.com/terryzh/archive/2012/11/10/2763538.html LinQ To Object 基本用法 inq的基本语法:var resu ...

  3. Linq的基本用用法

    Linq 的基本用法: Sort , OrderBy, Skip,Take,Where,Compare,Join,Distinct ,InsertRange 等关键词 Select用法 var sel ...

  4. 分享一个递归无限级拼接Json的方法---ExtJs的TreePanel和TreeGrid均适用(Ef,Lambda,Linq,IQueryable,List)

    话不多说,先上实体类,如果你不是codefirst,就把它当成数据表结构. 下面是底层BaseDal获取数据的方法  (如果你没有Base类,直接写在你的DAL层和BLL层) 下面是BaseServi ...

  5. SqlServer与Linq 无限递归目录树且输出层级

    ALTER VIEW [dbo].[view_TreeLevel] AS WITH cte AS ( SELECT a.ModuleID , a.Module_Name , a.Module_Desc ...

  6. LINQ如何递归子控件

    private void SetTextReadOnly(Control ctr, bool blReadOnly) { ctr.Controls.Cast<Control>().Sele ...

  7. C#递归解决汉诺塔问题(Hanoi)

    using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace MyExamp ...

  8. C#递归遍历子目录与子目录中的文件

    [转载]作者:weixingstudio 采用C#,通过指定一个路径,来递归的遍历所有的子目录以及子目录中的文件,建一个类似资源管理器的目录树 先递归的遍历所有的子目录,如果没有子目录以后,则遍历所有 ...

  9. .NET面试题系列[14] - LINQ to SQL与IQueryable

    .NET面试题系列目录 名言警句 "理解IQueryable的最简单方式就是,把它看作一个查询,在执行的时候,将会生成结果序列." - Jon Skeet LINQ to Obje ...

随机推荐

  1. Windows Server 2012网卡Teaming模式

    成组模式: Switch-independent(交换机独立): 这是配置时的默认值,此模式不要求交换机参与组合配置,由于独立模式下的交换机不知道网卡是主机上组合一部分,teaming组中的网卡可以连 ...

  2. 什么是IDOC,以及IDOC的步骤

    创建IDOC:   第一步:WE31 创建IDOC所包含的字段.   第二步:WE30 创建IDOC 把Segment分配给IDOC   第三步:WE81  创建信息类型   第四步:WE82   把 ...

  3. go strings 常用的几个函数

    fmt.Println(strings.ToUpper("hello world")) //转换为大写    fmt.Println(strings.ToLower("H ...

  4. 重现PHP Core的调用栈

        以前, 我曾经介绍过如何通过PHP的Core文件获取信息:如何调试PHP的Core之获取基本信息, 对于调用参数这块, 当时介绍的获取方法比较复杂. 于是今天我为PHP 5.4的.gdbini ...

  5. PHP内核探索之变量 图解

    http://blog.csdn.net/ohmygirl/article/details/41542445 http://www.laruence.com/2008/09/19/520.html

  6. JavaScript网站设计实践(四)编写about.html页面,利用JavaScript和DOM,选择性的显示和隐藏DIV元素

    一.现在我们在网站设计(三)的基础上,来编写about.html页面. 这个页面要用到的知识点是利用JavaScript和DOM实现选择性地显示和隐藏某些DIV about.html页面在前面我们为了 ...

  7. 按字母顺序排序的 arcpy.mapping 类列表

    arcpy.mapping 类可使用地图文档 (.mxd) 或图层文件 (.lyr) 中的不同对象类型的各种方法和属性.此文档可专门用作快速参考.有关详细信息,请使用链接跳转至各帮助页面. arcpy ...

  8. IE兼容低版本

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><m ...

  9. monkey与monkeyrunner的使用

    什么是Monkey Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中.它向系统发送伪随机的用户事件流(如按键输入.触摸屏输入.手势输入等),实现对正在开发的应用程序进行压 ...

  10. 面试之SQL

    1. 查询性能优化:从数据库查询数据时,你一定遇到过查询很慢的情况,请问你是怎么处理的. 答: 遇到的问题描述:是遇到过这种情况,我们给客户做过一款软件,日志库搜集了6000万条数据,显示.查询时候慢 ...