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
427 views
in Technique[技术] by (71.8m points)

c# - How to build a hierarchy with use Linq to object?

I have a list of data structures:

        public List<Personal> Personals()
        {
            return new List<Personal>
                {
                    new Personal
                        {
                            Id = 0,
                            Name = "Name 0"
                        },
                    new Personal
                        {
                            Id = 1,
                            Name = "Name 1",

                            ParentId = 0
                        },
                    new Personal
                        {
                            Id = 2,
                            Name = "Name 2",
                            ParentId = 0
                        },
                    new Personal
                        {
                            Id = 3,
                            Name = "Name 3",
                            ParentId = 0
                        },
                    new Personal
                        {
                            Id = 4,
                            Name = "Name 4",
                            ParentId = 1
                        },
                    new Personal
                        {
                            Id = 5,
                            Name = "Name 5",
                            ParentId = 1
                        },
                    new Personal
                        {
                            Id = 6,
                            Name = "Name 6",
                            ParentId = 2
                        },
                    new Personal
                        {
                            Id = 7,
                            Name = "Name 7",
                            ParentId = 2
                        },
                    new Personal
                        {
                            Id = 8,
                            Name = "Name 8",
                            ParentId = 4
                        },
                    new Personal
                        {
                            Id = 9,
                            Name = "Name 9",
                            ParentId = 4
                        },
                };
        }

and I want to build a tree:

public List<Tree> Trees()
            {
                return new List<Tree>
                    {
                        new Tree
                            {
                                Id = 0,
                                Name = "Name 0",
                                List = new List<Tree>
                                    {
                                        new Tree
                                            {
                                                Id = 1,
                                                Name = "Name 1",
                                                List = new List<Tree>
                                                    {
                                                        new Tree
                                                            {
                                                                Id = 4,
                                                                Name = "Name 4"
                                                            },
                                                        new Tree
                                                            {
                                                                Id = 5,
                                                                Name = "Name 5"
                                                            }
                                                    }
                                            }
                                    }
                            }
                    };
            }

How do you build a tree with LinQ to object? I have to use but it doesn't work exactly, see below:

public List<Tree> GetTree(List<Personal> list)
        {
            var listFormat = list.Select(x => new Tree
                {
                    Id = x.Id,
                    Name = x.Name,
                    ParentId = x.ParentId
                }).ToList();

            var lookup = listFormat.ToLookup(f => f.ParentId);
            foreach (var tree in listFormat)
            {
                tree.List = lookup[tree.Id].ToList();
            }

            return listFormat;
        }
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You should use recursion:

public void SomeMethod() {
     // here you get your `list`
     var tree = GetTree(list, 0);
}

public List<Tree> GetTree(List<Personal> list, int parent) {
    return list.Where(x => x.ParentId == parent).Select(x => new Tree {
        Id = x.Id,
        Name = x.Name,
        List = GetTree(list, x.Id)
   }).ToList();
}

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

...