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. 对PostgreSQL xmin的深入学习

    当PostgreSQL需要insert 一条记录的时候,它会把记录头放入xmin,xmax等字段. xmin的值,就是当前的Transaction的TransactionId.这是为了满足MVCC的需 ...

  2. libpq中调用prepared statement:

    代码如下: [root@lex tst]# cat testlibpq.c /* * testlibpq.c * Test the C version of LIBPQ, the POSTGRES f ...

  3. 图像的稀疏表示——ScSPM和LLC的总结

    前言 上一篇提到了SPM.这篇博客打算把ScSPM和LLC一起总结了.ScSPM和LLC其实都是对SPM的改进.这些技术,都是对特征的描述.它们既没有创造出新的特征(都是提取SIFT,HOG, RGB ...

  4. 关于自定义adapter使用getApplicationContext()影响主题

    最近弄了一个东西,listview+switch构成界面 关于android自定义adapter,继承自baseadapter,发现界面的switch开关主题变了想要的是浅色的主题,但是却发现变成了深 ...

  5. [AngularJS - thoughtram] Exploring Angular 1.3: Binding to Directive Controllers

    The post we have: http://www.cnblogs.com/Answer1215/p/4185504.html gives a breif introduce about bin ...

  6. SharePoint 2013+ Sqlserver 2014 Kerberos 配置传奇, 最终的解决方案 验证。

    SharePoint 2013+ Sqlserver 2014 Kerberos 配置传奇. 1,安装数据库,我就不多说安装,客户一定要注意. 我将参照以下实施例和账户. 2,建立DNS,假设没有DN ...

  7. VC 部署

    部署是分发要安装到其他计算机上的已完成应用程序或组件的过程. 当使用 Visual C++ 库提供的功能生成 C/C++ 应用程序时,该应用程序在运行时将依赖于那些库的存在. 为了让应用程序运行,它必 ...

  8. LINUX SHELL条件判断

    算术运算的条件判断 [] [[]]: -eq -ne -lt -le -gt -ge (( )):><>=<== [root@monitor ~]# if (( 2 == 3) ...

  9. [转]使用ADO.NET访问Oracle存储过程

    本文转自:http://www.cnblogs.com/datasky/archive/2007/11/07/952141.html 本文讨论了如何使用 ADO.NET 访问 Oracle 存储过程( ...

  10. win7 cmd命令行窗口 宽度无法变大 自由调整大小

    偶然遇到了这个问题,百度查到了解决方案,执行一个bat批处理命令. mode con lines=40 mode con cols=160 color 250 cls @cmd