Return multiple aggregate columns in LINQ

前端 未结 4 1292
孤城傲影
孤城傲影 2020-12-06 11:25

I would like to translate the following SQL into LINQ:

SELECT
    (Select count(BidID)) as TotalBidNum,
    (Select sum(Amount)) as TotalBidVal
FROM Bids
         


        
4条回答
  •  无人及你
    2020-12-06 11:58

    You can write this query using GroupBy. The Lambda expression is as follows:

        var itemsBid = db.Bids
                         .GroupBy( i => 1)
                         .Select( g => new
                         {
                              TotalBidVal = g.Sum(item => item.Amount), 
                              TotalBidNum = g.Count(item => item.BidId)
                         });
    

提交回复
热议问题