Return multiple aggregate columns in LINQ

前端 未结 4 1283
孤城傲影
孤城傲影 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 12:05

    You could try this out. The variable b is an entity (for every iteration) while ctx is an entityset which has the extension methods you need.

    var ctx = _dataContext.Bids;
    
    var result = ctx
        .Select( x => new
        {
            TotalBidVal = ctx.Sum  ( p => p.Amount ),
            TotalBidNum = ctx.Count( p => p.BidId  )
        } )
        .First();
    

提交回复
热议问题