Group by without a Group
-
In SQL you can write:
SELECT SUM(a) as T1, SUM(b) AS T2 FROM Table
But is it possible to write a From x in .. Group query *without* having a group by clause and group key?? What would be the LINQ equivalent to this? I've not seen anything like this in the MS examples.
'Howard
-
In SQL you can write:
SELECT SUM(a) as T1, SUM(b) AS T2 FROM Table
But is it possible to write a From x in .. Group query *without* having a group by clause and group key?? What would be the LINQ equivalent to this? I've not seen anything like this in the MS examples.
'Howard
Does this MSDN forum answer[^] help?
Life, family, faith: Give me a visit. From my latest post: "The themes and truths of the Jewish holidays follow God's complete plan for this world. They are the root from which Christianity sprang and the historical reasons the church had for leaving them behind were unsound." Judah Himango
-
Does this MSDN forum answer[^] help?
Life, family, faith: Give me a visit. From my latest post: "The themes and truths of the Jewish holidays follow God's complete plan for this world. They are the root from which Christianity sprang and the historical reasons the church had for leaving them behind were unsound." Judah Himango
Thanks but no, not quite! I know I can do .Sum() on a single value, e.g.
var T1 = db.Table.Select(o => o.a).Sum();
var T2 = db.Table.Select(o =gt; o.b).Sum();But obviously this is two queries to get two values, whereas my SQL is one query and therefore more efficient:
SELECT SUM(a) as T1, SUM(b) AS T2 FROM Table
At present this is as close as I can get, by using group by with a value that will create a single result row (this is VB, I could not figure out the C# syntax at all...
Dim query = From t in Table _
Group By dummy = false _
Into T1 = Sum(t.a), T2 = Sum(t.b) _
select T1, T2This does the same as the SQL query - but the "dummy = false" is a kludge I added to make Group By work.. hence the question!
'Howard
-
In SQL you can write:
SELECT SUM(a) as T1, SUM(b) AS T2 FROM Table
But is it possible to write a From x in .. Group query *without* having a group by clause and group key?? What would be the LINQ equivalent to this? I've not seen anything like this in the MS examples.
'Howard
If you start a query with Aggregate instead, you can do something like that. The query would be:
Aggregate t In Table Into T1 = Sum(t.a), T2 = Sum(t.b)
-
If you start a query with Aggregate instead, you can do something like that. The query would be:
Aggregate t In Table Into T1 = Sum(t.a), T2 = Sum(t.b)
Aha - so that's what the Aggregate keyword was for! Many thanks
'Howard
-
If you start a query with Aggregate instead, you can do something like that. The query would be:
Aggregate t In Table Into T1 = Sum(t.a), T2 = Sum(t.b)
Guess what.. the SQL generated by LINQ to SQL runs two queries, not one!! Maybe MS needs to work on the query engine some..
'Howard