简体   繁体   中英

How to perform addition in a Linq query

My query is like

var query = dbContext.table1.join(dbcontext.table2,i=>i.table1.id,j=>j.table2.id,
                 (i,j)=>new {
                     name = i.name,
                     hours = (new decimal?[]{ j.day1,j.day2,j.day3}.Sum()),
                     total = ???????
                 }).ToArray();

In the hours field I am getting the values of individual user's working hours for three days. In the "total" field I want to display the sum of all users' "hours" values.

Can you tell me how to get the "total" value?

var total = query.Sum(x => x.hours);

由于这个总数是针对结果集中所有行的,因此您不需要每一行一个值,而是一个代表整个数组聚合的值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM