views:

28

answers:

2

Hello everybody!

I want to translate this simple SQL statement to linq-to-sql:

SELECT SUM(field1), SUM(field2)
FROM TableName

The closest linq-to-sql is (i think) using the group by clause:

from tbl in TableName
group tbl by (1) into g
select new{ value1= g.Sum(p => p.field1), value2 = g.Sum(p => p.field2)};

As this does not produce the expected outcome, what must be changed??

Thanks in advance

A: 

That should produce the same result. In what way does it not?

KristoferA - Huagati.com
You 're right!! In deed it produces the correct result set!! ...where was i when debugged??hhhmmmm....
Savvas Sopiadis
A: 

You want it without the group by. The only way I can think of is this:

var result =  new {value1 = Table1.Sum(a => a.field1), value2 = Table1.Sum(a => a.field2)};

Which is a bit of a bummer as it emits 2 separate queries.

You can use linqpad: http://www.linqpad.net/ to find out what sql your linq is emitting.

burnside
In my case i cannot use this approach because the actual statement is far more complicated and thus would raise performance issues. But thanks very much, after all it's good to know!
Savvas Sopiadis