title: How to group table data and sum by multiple columns using LINQ or another approach
body: <p>I have a table containing data similar to the following:</p>
  
  <pre><code>Shift--Name----------Department---Time
  1      Employee1         360      100
  1      Employee1         372      50
  1      Employee1         385      300
  2      Employee2         301      0
  2      Employee2         301      0
  2      Employee2         301      0
  2      Employee2         305      0
  2      Employee2         305      0
  2      Employee2         305      0
  2      Employee2         305      0
  2      Employee2         305      0
  2      Employee2         320      0
  2      Employee2         333      0
  2      Employee2         350      30
  2      Employee2         350      35
  2      Employee2         350      90
  2      Employee2         350      0
  2      Employee2         350      0
  2      Employee2         350      90
  2      Employee2         350      0
  2      Employee2         350      0
  2      Employee2         350      150
  2      Employee2         350      50
  2      Employee2         350      0
  2      Employee2         350      60
  2      Employee3         302      0
  2      Employee3         305      0
  2      Employee3         305      3
  2      Employee3         365      0
  2      Employee3         365      0
  2      Employee3         365      0
  2      Employee3         365      10
  2      Employee3         365      1
  2      Employee3         365      0
  2      Employee3         365      0
  2      Employee3         365      10
  2      Employee3         365      5
  2      Employee4         314      0
  2      Employee4         314      0
  2      Employee4         314      100
  </code></pre>
  
  <p>I'm new to VB.NET but I need to output the above table grouping by shift, then by name, then by department, and then get the sum of the time for each group, like the following:</p>
  
  <pre><code>Shift--Name----------Department---Time
  1      Employee1         360      100
                           ***      100
  
  1      Employee1         372      50
                           ***      50
  
  1      Employee1         385      300
                           ***      300
         ***                        450
  ***                               450
  
  2      Employee2         301      0
  2      Employee2         301      0
  2      Employee2         301      0
                           ***      0
  
  2      Employee2         305      0
  2      Employee2         305      0
  2      Employee2         305      0
  2      Employee2         305      0
  2      Employee2         305      0
                           ***      0
  
  2      Employee2         320      0
                           ***      0
  
  2      Employee2         333      0
                           ***      0
  
  2      Employee2         350      30
  2      Employee2         350      35
  2      Employee2         350      90
  2      Employee2         350      0
  2      Employee2         350      0
  2      Employee2         350      90
  2      Employee2         350      0
  2      Employee2         350      0
  2      Employee2         350      150
  2      Employee2         350      50
  2      Employee2         350      0
  2      Employee2         350      60
                           ***      505
         ***                        505
  
  2      Employee3         302      0
                           ***      0
  
  2      Employee3         305      0
  2      Employee3         305      3
                           ***      3
  
  2      Employee3         365      0
  2      Employee3         365      0
  2      Employee3         365      0
  2      Employee3         365      10
  2      Employee3         365      1
  2      Employee3         365      0
  2      Employee3         365      0
  2      Employee3         365      10
  2      Employee3         365      5
                           ***      26
         ***                        29
  
  2      Employee4         314      0
  2      Employee4         314      0
  2      Employee4         314      100
                           ***      100
         ***                        100
  </code></pre>
  
  <p>So for employee1, I can see that they spent a total of 100 minutes in department 360, 50 minutes in department 372, and 300 minutes in department 385. The total time for Employee1 is 450 minutes. Since there is only one employee on first shift, the total minutes for first shift is 450.
  Any suggestions on how I can formulate a LINQ query to get the desired grouping?</p>
  
  <p>If there is a better way than using LINQ, I am open to hearing it, I just thought that LINQ would be the most versatile way to handle the problem that I can modify for other similar needs.</p>
  
  <p>Edit:</p>
  
  <p>I've tried the following query but don't fully understand how it works to get the results I need:</p>
  
  <pre><code>Dim TimeGroups =
      From j In TotalTimeResults
      Group By x = New With {
          Key .Shift = j.Field(Of String)("Shift"),
          Key .Name = j.Field(Of String)("Name"),
          Key .Time = j.Field(Of Integer)("Time")} Into g = Group
      Select New With {
          .Shift = x.Shift,
          .Name = x.Name,
          .Time = x.Time,
          .total = g.Sum(Function(r) r.Field(Of Integer)("Time"))
      }
  </code></pre>
  
  <p>The table I am using is just a VB.NET DataTable with the columns and rows as displayed.</p>