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 = , 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递归的更多相关文章
- 【Linq递归查找系列】
Linq递归查找: public IEnumerable<MenuInfo> GetTree(int id, IEnumerable<MenuInfo> lst) { var ...
- LinQ To Object 基本用法
http://www.cnblogs.com/terryzh/archive/2012/11/10/2763538.html LinQ To Object 基本用法 inq的基本语法:var resu ...
- Linq的基本用用法
Linq 的基本用法: Sort , OrderBy, Skip,Take,Where,Compare,Join,Distinct ,InsertRange 等关键词 Select用法 var sel ...
- 分享一个递归无限级拼接Json的方法---ExtJs的TreePanel和TreeGrid均适用(Ef,Lambda,Linq,IQueryable,List)
话不多说,先上实体类,如果你不是codefirst,就把它当成数据表结构. 下面是底层BaseDal获取数据的方法 (如果你没有Base类,直接写在你的DAL层和BLL层) 下面是BaseServi ...
- SqlServer与Linq 无限递归目录树且输出层级
ALTER VIEW [dbo].[view_TreeLevel] AS WITH cte AS ( SELECT a.ModuleID , a.Module_Name , a.Module_Desc ...
- LINQ如何递归子控件
private void SetTextReadOnly(Control ctr, bool blReadOnly) { ctr.Controls.Cast<Control>().Sele ...
- C#递归解决汉诺塔问题(Hanoi)
using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace MyExamp ...
- C#递归遍历子目录与子目录中的文件
[转载]作者:weixingstudio 采用C#,通过指定一个路径,来递归的遍历所有的子目录以及子目录中的文件,建一个类似资源管理器的目录树 先递归的遍历所有的子目录,如果没有子目录以后,则遍历所有 ...
- .NET面试题系列[14] - LINQ to SQL与IQueryable
.NET面试题系列目录 名言警句 "理解IQueryable的最简单方式就是,把它看作一个查询,在执行的时候,将会生成结果序列." - Jon Skeet LINQ to Obje ...
随机推荐
- Chart控件的多种使用方法
花了近一周时间专门研究.net 3.5平台提供的Chart控件的使用方法,感觉该控件的功能很强大,做出的图表效果也很美观,使用方法也并不复杂.如今先讲下Chart控件的部署及一些基本使用方法. 一.安 ...
- 【原创】OllyDBG 入门系列(一)-认识OllyDBG
****** http://blog.fishc.com/645.html 标 题: [原创]OllyDBG 入门系列(一)-认识OllyDBG作 者: CCDebuger时 间: 2006-0 ...
- 最全面的Android Intent机制讲解
对于大型软件开发经验较少的程序员来说,这可能是一个不太容易理解的抽象概念,因为它与我们平常使用的简单函数调用,或者通过库调用接口的方式不太一样.在 Intent 的使用中你看不到直接的函数调用,相对函 ...
- iOS开发——面试笔试精华(四)
面试笔试精华(四) 1. Object-C有多继承吗?没有的话用什么代替? 1> OC是单继承,没有多继承 2> 有时可以用分类和协议来代替多继承 2. ...
- Unity手游之路<七>角色控制器
我们要控制角色的移动,能够所有细节都由自己来实现.控制角色模型的移动,同一时候移动摄影机,改变视角.当然Unity也提供了一些组件,能够让我们做更少的工作,实现我们所期望的功能.今天我们就一起系统来学 ...
- 实例源码--Android通讯录源码
下载源码 技术要点: 1.通讯录联 系人的管理 2.接听.打电话 3.发短信 4. 源码带详细的 中文注释 ...... 详细介绍: 1.通讯录联系人的管理 播放器具有播放本地音乐的功能 ...
- javascript 标签 src 链接动态文件。
test.html中的代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...
- Socket异步通信学习三
接下来是客户端部分,采用同步接收模式,在SocketClient项目中新建了一个SynServer类,用于存放socket服务器代码,和AsynServer类似,主要有4个方法: 有一个全局socke ...
- iOS 中使用.9图
背景 .9图来源于Android.为了设计出一套图,兼容Android和iOS,使用.9图的方式来对图片进行拉伸以适应不同的屏幕.在iOS中没有.9图的概念,只能先了解Android的.9图再进行模拟 ...
- Android Partitions Explained: boot, system, recovery, data, cache & misc
from: http://www.addictivetips.com/mobile/android-partitions-explained-boot-system-recovery-data-cac ...