简体   繁体   中英

Replacing SQL join and group query with linq query in Entity Framework

I have an existing SQL query:

select 
    mn.accountid, sum(u.peak), sum(u.text), sum(u.datagig),
    sum(p.minutesallowed), sum(p.messagesallowed), sum(p.dataallowed)
from 
    usage u
inner join 
    mtn mn on u.mtnid = mn.Id
inner join 
    [plan] p on p.id = mn.planid
group by 
    mn.accountid

Using Entity Framework, I'm working on converting this to linq, and I've gotten this far:

var tots = from u in currentUsage
   join mn in mtns on u.mtnId equals mn.Id
   join p in plans on mn.PlanId equals p.Id
   group u by u.AccountId into g
   select new MainUsageResults
   {
       TotalPeakMinutes = g.Sum(x => x.Peak),
       TotalData = g.Sum(x => x.DataGig),
       TotalMessaging = g.Sum(x => x.Text),
       TotalAllowedMinutes = g.Sum(x =>  ???) ,
       TotalAllowedMessages = g.Sum(x =>  ???) ,
       TotalAllowedData = g.Sum(x =>  ???) 
   };

I can't figure out how to sum up the data that is on the joined tables. In SQL one would have the whole set of columns available in a join, but it doesn't seem to be the case here. How do I get the sum of the columns on the joined tables in this example?

感谢@ 3-14159265358979323846264,可以在这里找到一个很好的例子

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