title: Optimize or ditch LINQ query?
body: <p>So I have a LINQ (to SQL) query that pulls information from a database into a grid.  There is a function to aggregate the grid data based on the current filter parameters which will sum the amount of recurring "X"'s in the grid data.</p>
  
  <p>For instance, lets assume the grid displays customer vists to a grocery store.  The original data may show the follow:</p>
  
  <pre><code>Date     | Name       | No. Prod   | Total $
  --------------------------------------------
  01/02/13 | Customer A | 4 products | $23.00 
  01/02/13 | Customer B | 2 products | $3.26
  01/02/13 | Customer C | 7 products | $47.42
  01/16/13 | Customer A | 3 products | $26.22
  </code></pre>
  
  <p>Clicking the summation function for the clients column will display the following grid data:</p>
  
  <pre><code>Cnt| Name       | Tot. Prod  | Total $  
  --------------------------------------
  2  | Customer A | 7 products | $49.22 
  1  | Customer B | 2 products | $3.26
  1  | Customer C | 7 products | $47.42
  </code></pre>
  
  <p>My problem is that I am doing the summation logic in a LINQ query.  I assumed this would be fast...but it is just the opposite.  Here is a sample.</p>
  
  <pre><code>Expression&lt;Func&lt;OrdersView, bool&gt;&gt; filter;
  filter = m =&gt; m.RecordCreated &gt;= fromDate &amp;&amp; m.RecordCreated &lt;= toDate &amp;&amp; m.DepartmentID == _depID;
  
  var ClientAggOrders = dataContext.OrdersView
      .Where(filter)
      .GroupBy(m =&gt; m.Name)
      .Select(gr =&gt; new
          {
              Name = gr.Key,
              Count = gr.Where(s =&gt; s.ID != null).Count(),
              id = gr.Select(s =&gt; s.ID),
              S1 = gr.Sum(s =&gt; s.Tare &lt; s.Gross ? s.Tare : s.Gross),
              S2 = gr.Sum(s =&gt; s.Tare &lt; s.Gross ? s.Gross : s.Tare),
              NetWeight = gr.Sum(s =&gt; s.NetWeight),
              Price = gr.Sum(s =&gt; s.NetPrice)
          }
      ).ToList();
  </code></pre>
  
  <p>My question is, why is this such bad practice?  LINQ allows for these expressions in the SELECT clause, but the time it takes to execute is beyond absurd to the point where I don't see it being beneficial in any real world scenario.  </p>
  
  <p>Am I using LINQ wrong and should I just move my logic outside of the query or can this be optimized and done within LINQ properly?  Thanks for any advice!</p>