title: How to build a hierarchy with use Linq to object?
body: <p>I have a list of data structures:</p>
  
  <pre><code>        public List&lt;Personal&gt; Personals()
          {
              return new List&lt;Personal&gt;
                  {
                      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
                          },
                  };
          }
  </code></pre>
  
  <p>and I want to build a tree:</p>
  
  <pre><code>public List&lt;Tree&gt; Trees()
              {
                  return new List&lt;Tree&gt;
                      {
                          new Tree
                              {
                                  Id = 0,
                                  Name = "Name 0",
                                  List = new List&lt;Tree&gt;
                                      {
                                          new Tree
                                              {
                                                  Id = 1,
                                                  Name = "Name 1",
                                                  List = new List&lt;Tree&gt;
                                                      {
                                                          new Tree
                                                              {
                                                                  Id = 4,
                                                                  Name = "Name 4"
                                                              },
                                                          new Tree
                                                              {
                                                                  Id = 5,
                                                                  Name = "Name 5"
                                                              }
                                                      }
                                              }
                                      }
                              }
                      };
              }
  </code></pre>
  
  <p>How do you build a tree with LinQ to object? I have to use but it doesn't work exactly, see below:</p>
  
  <pre><code>public List&lt;Tree&gt; GetTree(List&lt;Personal&gt; list)
          {
              var listFormat = list.Select(x =&gt; new Tree
                  {
                      Id = x.Id,
                      Name = x.Name,
                      ParentId = x.ParentId
                  }).ToList();
  
              var lookup = listFormat.ToLookup(f =&gt; f.ParentId);
              foreach (var tree in listFormat)
              {
                  tree.List = lookup[tree.Id].ToList();
              }
  
              return listFormat;
          }
  </code></pre>