Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
813 views
in Technique[技术] by (71.8m points)

c# - Recursive LINQ query: select item and all children with subchildren

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
   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);
                }
            }
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...