Matan L Matan L - 2 months ago 26
C# Question

LINQ group by and SUM

Possible Duplicate:

Get sum of two columns in one LINQ query

I need to know how to write an LINQ Query that will give me the same results as this SQL Query

select sum(col1) as [col1] , sum(col2) as [col2] , sum(col1)/sum(col2) as [col3], col4 as [col4]
from some_table
group by col4


Full props go to Steven for this answer:

from row in some_table
group row by row.col4 into g
select new
    col1 = g.Sum(x => x.col1), 
    col2 = g.Sum(x => x.col2),
    col3 = g.Sum(x => x.col1) / g.Sum(x => x.col2),
    col4 = g.Key