Data Browser - Viewing Site  Sector 23 Code Bank Logged in as:  Guest  


Returning Multiple Aggregates in Group by in LINQ
Another LINQ limitation?

If I try to compute two aggregate results at once over the same group, such as:

var query = from d in dataContext.Details join
o in dataContext.Orders
on d.Order equals o
group d by d.OrderType into orderTypeGroup
select new
Total1 = orderTypeGroup.Count(d => d.Field <= 1),
Total2 = orderTypeGroup.Count(d => d.Field == 2),
Total3 = orderTypeGroup.Count(d => d.Field > = 3)

Instead of grouping the table once and computing the 3 sums, it seems that LINQ groups the table THREE separate times, computing each of the sums one at a time. Doesn't this result in poor performance? It seems there should be some way to replicate the appropriate SQL without having to resort to a view or stored procedure. Another option is to load the grouped data in memory and perform the counts/sums/etc in memory, but that kind of operation should be kept on the db!

Created By: amos 9/12/2008 5:22:32 PM
Updated: 9/12/2008 5:22:50 PM