I have this table: Statistics (id int, col1 int, col2 int, col3 int ...) I want to get the sum of col1 values, col2 values, col3 values, etc. Simple SQL query:
SELECT SUM([Col1]), SUM([Col2]) FROM [dbo].[Statistics]
but in LINQ:
var val1 = db.Statistics.Sum(x => x.Col1); var val2 = db.Statistics.Sum(x => x.Col2); var val3 = db.Statistics.Sum(x => x.Col3); ...
Thus, it works, but it executes N queries in the database. I would like to complete only one. The only way I found is this:
var result = db.Statistics.GroupBy(x => 1).Select(x => new { val1 = x.Sum(k => k.Col1), val2 = x.Sum(k => k.Col2), val3 = x.Sum(k => k.Col3), });
that it generates a complex query. this is normal?
UPDATE is 2 execution plans:

source share