简体   繁体   中英

Converting SQL query into Linq which used complex sum with group by

Here is my Service table's columns:

id
serverPlace
count

Here is a SQL query which I need to convert it into Linq:

 SELECT Service.id,
    Service.serverPlace,
    sum(Service.count * 12 + MONTH(getdate())) AS sum
   FROM Service
  GROUP BY Service.id, Service.serverPlace;

I do not know how to convert and implement sum section with Linq.

It should look like this:

var month = DateTime.Now.Month;
var result = await dbContext.Services.GroupBy(r=> new { r.Id, r.ServerPlace })
            .Select(r=> new {
             r.Key.Id,
             r.Key.ServerPlace,
             Sum = r.Sum(q=> q.Count*12 + month)
            }).ToArrayAsync();

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