title: Roll up totals of an ObservableCollection<MyType> with a Linq expression
body: <p>I have a Silverlight delegate which gets an ObservableCollection in the EventArgs Result. The DTO myType has fields Order, StartDate, Status, PlannedAmount, ActualAmount, and few others. The query on the WCF service side gets several rows per Order, varying only by PlannedAmount and ActualAmount.</p>
  
  <pre><code>Order # | StartDate | PlannedAmount| ActualAmount | Order Comments ....
  Order A | March 15  |    20.00     |     0.00     | Comment 1 ...
  Order A | March 15  |    30.00     |     0.00     | Comment 1 ...
  Order A | March 15  |    10.00     |     0.00     | Comment 1 ...
  Order A | March 15  |     0.00     |    30.00     | Comment 1 ...
  Order B | March 25  |    10.00     |      0       | Comment 2 ...
  Order B | March 25  |     0.00     |     5.00     | Comment 2 ...
  </code></pre>
  
  <p>I would like to show only one row per Order and sum all the PlannedAmount and ActualAmount values. I prefer to change this in the presentation layer as I don't know what other consumers of the WCF Operation require. So I want to roll it up to...</p>
  
  <pre><code>Order # | StartDate | PlannedAmount| ActualAmount | Order Comments ....
  Order A | March 15  |    60.00     |    30.00     | Comment 1 ...
  Order B | March 25  |    10.00     |    5.00      | Comment 2 ...
  </code></pre>
  
  <p>And then make this an ObservableCollection of the same type that I had before.</p>
  
  <p>I have tried this but I cannot seem to get anything but the Key and the Sum values.</p>
  
  <pre><code>    var Orders =
          from wo in OrdersPerOperation
          group wo by wo.OrderNo
          into g
          select new
          {
              OrderNo = g.Key,
              Planned = g.Sum(wo =&gt; wo.Planned),
              Actual = g.Sum(wo =&gt; wo.Actual),
              OrderComments = g.Select(wo =&gt; wo.Equipment),
              StartDate = g.Select(wo =&gt; wo.StartDate),
              Status = g.Select(wo =&gt; wo.Status),
              OrderType = g.Select(wo =&gt; wo.OrderType) //,...
          };
  </code></pre>
  
  <p><strong>Edit</strong><br>
  Getting just the key and two sums is straight-forward:</p>
  
  <pre><code>var Orders =
          from wo in OrdersPerOperation
          group wo by wo.OrderNo
          into g
          select new
          {
              OrderNo = g.Key,
              Planned = g.Sum(wo =&gt; wo.Planned),
              Actual = g.Sum(wo =&gt; wo.Actual)
          }
  </code></pre>
  
  <p>The challenge is getting all of the other fields, which are repetitive to also show in the result.</p>
  
  <p><strong>EDIT</strong><br>
  I made this guess that this might work like a self-referencing SQL query. I believe I am on the right track with this, in that each element has data in the correct format. I still cannot set the results to the ItemsSource of the Silverlight grid, which I could bind, before all of this Linq confusion. The IDE warns me that FirstOrDefault is a possible NullReferenceException.</p>
  
  <pre><code>var workOrders = from wo in workOrdersPerOperation
                  group wo by wo.OrderNo
                  into g
                  select new
                  {
                      OrderNo = g.Key,
                      Planned = g.Sum(wo =&gt; wo.Planned),
                      Actual = g.Sum(wo =&gt; wo.Actual),
                      g.FirstOrDefault(wo =&gt; wo.OrderNo == g.Key).Location,
                      g.FirstOrDefault(wo =&gt; wo.OrderNo == g.Key).Equipment,
                      g.FirstOrDefault(wo =&gt; wo.OrderNo == g.Key).StartDate,
                      g.FirstOrDefault(wo =&gt; wo.OrderNo == g.Key).Status,
                      g.FirstOrDefault(wo =&gt; wo.OrderNo == g.Key).OrderType,
                      g.FirstOrDefault(wo =&gt; wo.OrderNo == g.Key).AccType,
                      g.FirstOrDefault(wo =&gt; wo.OrderNo == g.Key).WorkCenter,
                      g.FirstOrDefault(wo =&gt; wo.OrderNo == g.Key).Description,
                      g.FirstOrDefault(wo =&gt; wo.OrderNo == g.Key).Priority
                  };
  </code></pre>
  
  <p>Can anyone get me past this step? I still need to bind it to the control.</p>