简体   繁体   English

在Entity Framework中用linq查询替换SQL连接和组查询

[英]Replacing SQL join and group query with linq query in Entity Framework

I have an existing SQL query: 我有一个现有的SQL查询:

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: 使用实体框架,我正在将其转换为linq,到目前为止,我已经做到了:

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. 在SQL中,连接中可以使用整个列集,但在这里似乎并非如此。 How do I get the sum of the columns on the joined tables in this example? 在此示例中,如何获取联接表上的列总和?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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