title: Linq Recursive Sum
body: <p>I have the following data strucure:</p>
  
  <pre><code>        List&lt;Item&gt; Items = new List&lt;Item&gt;
          {
              new Item{ Id = 1, Name = "Machine" },
              new Item{ Id = 3, Id_Parent = 1,  Name = "Machine1"},
              new Item{ Id = 5, Id_Parent = 3,  Name = "Machine1-A", Number = 2, Price = 10 },
              new Item{ Id = 9, Id_Parent = 3,  Name = "Machine1-B", Number = 4, Price = 11 },
              new Item{ Id = 100,  Name = "Item" } ,
              new Item{ Id = 112,  Id_Parent = 100, Name = "Item1", Number = 5, Price = 55 }
          };
  </code></pre>
  
  <p>I want to build a query that gets the sum of all children price in its parent (items are related by Id_Parent).
  For example, for Item Id = 100, I have 55, because thats the value of the its child .</p>
  
  <p>For Item Id = 3 I have 21, becaue Item Id = 5 and Id = 9 all sum to that.
  So far soo good.</p>
  
  <p>What I am strugling to get is for Item Id = 1 I should also have the sum = 21, because Id = 3 is a child of Id = 1 and it has a sum of 21.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>        var result = from i in items
                                     join item in item on i.Id_Parent equals item.Id
                                     select new
                                     {
                                         Name = prod.Nome,
                                         Sum =
                                           (from it in items
                                            where it.Id_Parent == item.Id
                                            group it by new
                                            {
                                                it.Id_Parent
                                            }
                                            into g
                                            select new
                                            {
                                                Sum = g.Sum(x =&gt; x.Price)
                                            }
                                           ).First()
                                     };
  </code></pre>
  
  <p>Help appreciated.</p>