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 ...
随机推荐
- 判断一个Bitmap图像是否是.9图
见BitmapFactory的源码中 byte[] np = bm.getNinePatchChunk(); final boolean isNinePatch = np != null & ...
- 【转】CppUnit使用简介
以下内容来自:http://www.cnblogs.com/wishma/archive/2008/08/01/1258370.html 1. 简介 CppUnit 是个基于 LGPL 的开源项目,最 ...
- IE下载打印文件的时候,下载打印闪一下就没有了
这是因为我们的浏览器没有将文件下载的自动提示设为启用.点击IE菜单栏中的“工具”—“Internet选项”-安全—可信站点—自定义级别 1,添加信任站点 打开IE浏览器,输入需要下载文件的地址 选择[ ...
- C#-设置窗体在显示器居中显示
在窗体的属性中查看:StartPosition属性,该属性的设置中有一个"CenterScreen"的选择项,该项就是设置窗体局中显示的.
- go strings 常用的几个函数
fmt.Println(strings.ToUpper("hello world")) //转换为大写 fmt.Println(strings.ToLower("H ...
- js Uncaught SyntaxError: Unexpected token错误
今天遇到js报错Uncaught SyntaxError: Unexpected token 不知道是什么原因,并且js还会继续往下执行. 经过排查竟然是在保存行的上面有个if少一个大括号,真是坑爹啊 ...
- 基于cocos2d-x的游戏框架设计——李成
视频:http://v.youku.com/v_show/id_XMzc5ODUyMTI4.html?f=17330006 网易科技讯 3月31日,第四届CocoaChina开发者大会暨Cocos2d ...
- Mailing API
Mailing API Configuration Basic Usage Embedding Inline Attachments Mail & Local Development Work ...
- 说说log4cplus
<C++ primer 第五版>已经翻了一段时间了,每天早上的班车上看一个小时.书是好书,可惜很多知识还是停留在表面上.每天除了翻书,一是也找到不合适的方法进一步深入,晚上看到新闻联播的老 ...
- IPC with pipes, also dup2 redirect stream handle
#include <stdio.h> #include <sys/types.h> #include <sys/wait.h> #include <unist ...