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 ...
随机推荐
- 【转】Netty那点事(三)Channel中的Pipeline
[原文]https://github.com/code4craft/netty-learning/blob/master/posts/ch3-pipeline.md Channel是理解和使用Nett ...
- SVN的版本日期
SVN还可以使用版本日期来指定某个版本,日期格式使用ISO-8601标准,一般是yyyy-mm-dd hh:mm:ss.当你指定一个日期,SVN会在版本库中找到最接近这个日期的版本. SVN对日期的解 ...
- ALERT日志中常见监听相关报错之中的一个:ORA-609错误的排查
參考MOS文档有: Troubleshooting Guide ORA-609 : Opiodr aborting process unknown ospid (文档 ID 1121357.1) Al ...
- Linux-PAM(Linux下的密碼認證和安全机制)系統管理員指南(中文版)
he Linux-PAM 系统管理员指南作者:Andrew G. Morgan, morgan@linux.kernel.org翻译:孙国清(Thomas Sun),thomassun@yeah.ne ...
- [Effective C++ --007]为多态基类声明virtual析构函数
引言: 我们都知道类的一个很明显的特性是多态,比如我们声明一个水果的基类: class Fruit { public: Fruit() {}; ~Fruit(){}; } 那么我们根据这个Fruit基 ...
- 信号之sigsuspend函数
更改进程的信号屏蔽字可以阻塞所选择的信号,或解除对它们的阻塞.使用这种技术可以保护不希望由信号中断的代码临界区.如果希望对一个信号解除阻塞,然后pause等待以前被阻塞的信号发生,则又将如何呢?假定信 ...
- Android之UI--重绘EditText以及实现Button的渐变色
在本文中实现的是比较普遍的一个对EditText的重绘以及对于按钮或窗口添加渐变色. 因为EditText是继承于TextView的,所以可以实现对EditText的重绘,在重绘的时候只需要继承Edi ...
- Matlab图像函数之pie
一.pie pie用于描绘平面饼图. (1)pie(X) 利用向量X中的数据描绘饼图. 例如: X = [1, 1, 2, 2, 3, 4, 5]; pie(X) 得到 注意,X中的数据被看做频数,饼 ...
- algorithms中计算时间的渐近表示
1.大写Ο符号大写Ο符号给出了函数f的一个上限. 定义[大写Ο符号]:f(n)=Ο(g(n)),当且仅当存在正的常数c和n0,使得对于所有的n≥n0,有 f(n)≤c*g(n) 上述定义表明,函数f至 ...
- Win8.1中打不开IE的解决方法
IE11 打不开了,重新安装了IE也解决不了问题,咋整,找了好久终于找到原因,原来是权限的问题,特此和大家共享 解决办法: WIN+R,regedit,找到HKEY_CURRENT_USER\Soft ...