I've this table: Statistics(id int, col1 int, col2 int, col3 int ...) I want to get the sum of the col1 values, col2 values, col3 values etc. A 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);
...
In this way works, but this executes N queries on the database. I would like execute only one. The only way that 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 generate a complex query. is it normal?
UPDATE these are the 2 execution plans: