Linq to SQL: how to aggregate without a group by?

后端 未结 2 1890
北海茫月
北海茫月 2020-12-05 13:09

I am searching for the Linq-to-SQL equivalent to this query:

SELECT
  [cnt]=COUNT(*),
  [colB]=SUM(colB),
  [colC]=SUM(colC),
  [colD]=SUM(colD)
FROM myTable         


        
2条回答
  •  猫巷女王i
    2020-12-05 13:51

    You can do the same query using Lambda expression as follows:

      var orderTotals = db.Orders
                          .GroupBy( i => 1)
                          .Select( g => new
                          {
                               cnt = g.Count(), 
                               ScolB = g.Sum(item => item.ColB), 
                               ScolC = g.Sum(item => item.ColC) 
                          });
    

提交回复
热议问题