title: Complex linq statement - nested groups/where/sum/select
body: <p>I have an object such as the sample below</p>
  
  <pre><code>object = {
          [0] {Name: "Foo", Customer: "Alex", Number: "33.2", Amt: 100.50, FileId: 12345611, FileNumber: "0987-123"}
          [1] {Name: "Bar", Customer: "Josh" Number: "1", Amt: 100.50, FileId: 456546456, FileNumber: "0987-123"}
          [2] {Name: "Foo", Customer: "Alex", Number: "33.2", Amt: 110.00, FileId: 12345611, FileNumber: "0987-123"}
          [3] {Name: "Foo", Customer: "Frank", Number: "9.8", Amt: 100.50, FileId: 324234, FileNumber: "9999-123"}
          [4] {Name: "Test2", Customer: "Bob" , Number: "1.9", Amt: 100.50, FileId: 782724, FileNumber: "0987-123"}
          [5] {Name: "Foo", Customer: "Alex" , Number: "33.2", Amt: 50.80, FileId: 9872561, FileNumber: "8181-1273"}
      }
  </code></pre>
  
  <p>I need it to look like the below</p>
  
  <pre><code>groupedObject = {
          [0] {Name: "Foo", 
                  Rows: {
                          row: {
                              [0] {Number: "33.2", Customer: "Alex", Details: {
                                          [0] {Amt: 210.50, Number: "33.2", FileId: 12345611, FileNumber: "0987-123"}
                                          [0] {Amt: 50.80, Number: "33.2", FileId: 9872561, FileNumber: "8181-1273"}
                                      }
                                  }
                              [1] {Number: "9.8", Customer: "Frank", Details: {
                                          [0] {Amt: 100.50, Number: "9.8", FileId: 324234, FileNumber: "9999-123"}
                                      }
                                  }
                          }
                      }
              }
          [1] {Name: "Bar", 
                  Rows: {
                          row: {
                              [0] {Number: "33.2", Customer: "Josh", Details: {
                                          [0] {Amt: 100.50, Number: "1", FileId: 456546456, FileNumber: "0987-123"}
                                      }
                                  }
                          }
                      }
              }
          [2] {Name: "Test2", 
                  Rows: {
                          row: {
                              [0] {Number: "1.9", Customer: "Bob", Details: {
                                          [0] {Amt: 100.50, Number: "1.9", FileId: 782724, FileNumber: "0987-123"}
                                      }
                                  }
                          }
                      }
              }
      }   
  </code></pre>
  
  <p>What I am trying to accomplish is to group by Name, which is then grouped by Customer within the name, and then the customer details within that customer, and details with the same fileID need the rows combined. Is this possible with linq?</p>
  
  <p>If it helps with visualization below is what I need, but in XML.</p>
  
  <pre><code>&lt;main&gt;
      &lt;UniqueNames&gt;
          &lt;randominfo&gt;true&lt;/randominfo&gt;
          &lt;randominfo2&gt;false&lt;/randominfo2&gt;
          &lt;head&gt;
              &lt;Name&gt;Foo&lt;/Name&gt;
          &lt;/head&gt;
          &lt;rows&gt;
              &lt;row&gt;
                  &lt;Number&gt;"33.2"&lt;/Number&gt;
                  &lt;Customer&gt;"Alex"&lt;/Customer&gt;
                  &lt;Details&gt;
                      &lt;Detail&gt;
                          &lt;Amt&gt;210.50&lt;/Amt&gt;
                          &lt;Number&gt;"33.2"&lt;/Number&gt;
                          &lt;FileId&gt;12345611&lt;/FileId&gt;
                          &lt;FileNumber&gt;"0987-123"&lt;/FileNumber&gt;
                      &lt;/Detail&gt;
                      &lt;Detail&gt;
                          &lt;Amt&gt;50.80&lt;/Amt&gt;
                          &lt;Number&gt;"33.2"&lt;/Number&gt;
                          &lt;FileId&gt;9872561&lt;/FileId&gt;
                          &lt;FileNumber&gt;"8181-1273"&lt;/FileNumber&gt;
                      &lt;/Detail&gt;
                  &lt;/Details&gt;
              &lt;/row&gt;
          &lt;/rows&gt;
      &lt;/UniqueNames&gt;
      &lt;UniqueNames&gt;
          &lt;randominfo&gt;true&lt;/randominfo&gt;
          &lt;randominfo2&gt;false&lt;/randominfo2&gt;
          &lt;head&gt;
              &lt;Name&gt;Bar&lt;/Name&gt;
          &lt;/head&gt;
          &lt;rows&gt;
              &lt;row&gt;
                  &lt;Number&gt;"33.2"&lt;/Number&gt;
                  &lt;Customer&gt;"Josh"&lt;/Customer&gt;
                  &lt;Details&gt;
                      &lt;Detail&gt;
                          &lt;Amt&gt;100.50&lt;/Amt&gt;
                          &lt;Number&gt;"1"&lt;/Number&gt;
                          &lt;FileId&gt;456546456&lt;/FileId&gt;
                          &lt;FileNumber&gt;"0987-123"&lt;/FileNumber&gt;
                      &lt;/Detail&gt;
                  &lt;/Details&gt;
              &lt;/row&gt;
          &lt;/rows&gt;
      &lt;/UniqueNames&gt;
      &lt;UniqueNames&gt;
          &lt;randominfo&gt;true&lt;/randominfo&gt;
          &lt;randominfo2&gt;false&lt;/randominfo2&gt;
          &lt;head&gt;
              &lt;Name&gt;Test2&lt;/Name&gt;
          &lt;/head&gt;
          &lt;rows&gt;
              &lt;row&gt;
                  &lt;Number&gt;"1.9"&lt;/Number&gt;
                  &lt;Customer&gt;"Bob"&lt;/Customer&gt;
                  &lt;Details&gt;
                      &lt;Detail&gt;
                          &lt;Amt&gt;100.50&lt;/Amt&gt;
                          &lt;Number&gt;"1.9"&lt;/Number&gt;
                          &lt;FileId&gt;782724&lt;/FileId&gt;
                          &lt;FileNumber&gt;"0987-123"&lt;/FileNumber&gt;
                      &lt;/Detail&gt;
                  &lt;/Details&gt;
              &lt;/row&gt;
          &lt;/rows&gt;
      &lt;/UniqueNames&gt;
  &lt;/main&gt;
  </code></pre>
  
  <p>I've tried the below...
  Grouping my Object by the name, and then listing each object for each name..</p>
  
  <pre><code>var testgroup = TestObject.GroupBy( item =&gt; item.Name)
      .Select(group =&gt; new { Name = group.Key, Items = group.ToList() })
      .ToList()
  </code></pre>
  
  <p>Then I also have another one for getting all the details</p>
  
  <pre><code>var Details =
                  (from c in TestObject
                      group c by new
                      {
                          c.Number,
                          c.FileId,
                          c.FileNumber,
                      } into cFIDs
                      select new FileAllocation()
                      {
  
                          FileAmount = cFIDs.Sum(s =&gt; s.Amt),
                          Number = cFIDs.Key.Number,
                          FileId = cFIDs.Key.FileId,
                          FileNumber = cFIDs.Key.FileNumber,
                      }).ToList();
  </code></pre>
  
  <p>The above allowed me to group and generate the details I needed, and the first one has the group by name and the listed rows for each... I also tried the below.</p>
  
  <p>I'm just not sure how to put it all together</p>