Get sum of two columns in one LINQ query

后端 未结 9 1001
猫巷女王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:42

    To sum the table, group by a constant:

    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)
    }
    

提交回复
热议问题