递归LINQ查询:选择项目,并与subchildren所有儿童 [英] Recursive LINQ query: select item and all children with subchildren
本文介绍了递归LINQ查询:选择项目,并与subchildren所有儿童的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法写一个LINQ(或程序式的)查询,可以选择一个项目,并与一个查询所有的孩子?
我有实体:
公共类评论
{
公众诠释标识{搞定;设置;}
公众诠释的ParentId {获取;设置;}
公众诠释文本{获取;集;}
}
我有一个ID,所以我要选择具有ID及其所有儿童subchildren评论。
示例:
1
-2
--3
-4
-5
--6
2
3
如果ID == 1,那么我想的1,2,3,4,5,6列表。
解决方案
公共类评论
{
公众诠释标识{搞定;组; }
公众诠释的ParentId {搞定;组; }
公共字符串文本{搞定;组; }
公开名单<&评论GT;儿童{搞定;组; }
}
类节目
{
静态无效的主要()
{
名单,LT;注释>类别=新的List<注释>()
{
新评论(){ID = 1,文本=项目1的ParentId = 0},
新评论(){ID = 2,文本=项目2的ParentId = 0},
新评论(){ID = 3,文本=3项的ParentId = 0},
新评论(){ID = 4,文本=项目1.1的ParentId = 1},
新评论(){n = 5,文本=项目3.1的ParentId = 3},
新评论(){ID = 6,文本=1.1.1项的ParentId = 4},
新评论(){n = 7,文本=项目2.1的ParentId = 2}
};
名单,LT;注释>等级=新的List<&评论GT;();
等级=类别
。凡(C => c.ParentId == 0)
。选择(C =>新建评论(){
n = c.Id ,
文本= c.Text,
的ParentId = c.ParentId,
=儿童的GetChildren(类别,c.Id)})
.ToList();
HieararchyWalk(层次);
到Console.ReadLine();
}
公共静态列表<&评论GT;的GetChildren(列表<注释>的意见,INT parentId的)
{
返回评论
。凡(C => c.ParentId == parentId的)
。选择(C => ;新的注释{
n = c.Id,
文本= c.Text,
的ParentId = c.ParentId,
=儿童的GetChildren(注释,c.Id)})
.ToList();
}
公共静态无效HieararchyWalk(列表<注释>层次)(!等级= NULL)
{
如果
{
的foreach (在层次结构VAR项)
{
Console.WriteLine(的String.Format({0} {1},item.Id,item.Text));
HieararchyWalk(item.Children);
}
}
}
Is there any way to write a LINQ (or procedural style) query, that can select an item and all children with one query? I have entity:
public class Comment
{
public int Id {get;set;}
public int ParentId {get;set;}
public int Text {get;set;}
}
I have an ID, so I want to select Comment with ID and all its children with subchildren. Example:
1
-2
--3
-4
-5
--6
2
3
If ID == 1 then I want list of 1,2,3,4,5,6.
解决方案
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 = 1, Text = "Item 1", ParentId = 0},
new Comment() { Id = 2, Text = "Item 2", ParentId = 0 },
new Comment() { Id = 3, Text = "Item 3", ParentId = 0 },
new Comment() { Id = 4, Text = "Item 1.1", ParentId = 1 },
new Comment() { Id = 5, Text = "Item 3.1", ParentId = 3 },
new Comment() { Id = 6, Text = "Item 1.1.1", ParentId = 4 },
new Comment() { Id = 7, Text = "Item 2.1", ParentId = 2 }
};
List<Comment> hierarchy = new List<Comment>();
hierarchy = categories
.Where(c => c.ParentId == 0)
.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查询:选择项目,并与subchildren所有儿童的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文