Get sum of two columns in one LINQ query

后端 未结 9 999
猫巷女王i
猫巷女王i 2020-12-01 09:04

let\'s say that I have a table called Items (ID int, Done int, Total int)

I can do it by two queries:

int total = m.Items.Sum(p=>p.Total)
int done         


        
9条回答
  •  心在旅途
    2020-12-01 09:30

    This will do the trick:

    from p in m.Items
    group p by 1 into g
    select new
    {
        SumTotal = g.Sum(x => x.Total), 
        SumDone = g.Sum(x => x.Done) 
    };
    

提交回复
热议问题