在实体框架中用linq查询替换SQL联接和组查询
本文关键字:查询 SQL 替换 框架 linq 实体 | 更新日期: 2023-09-27 18:00:06
我有一个现有的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
使用实体框架,我正在将其转换为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 => ???)
};
我不知道如何汇总联接表上的数据。在SQL中,联接中会有一整列可用,但这里的情况似乎并非如此。在本例中,如何获得联接表上的列的总和?
感谢@3-14159265358979323846264,在这里可以找到一个真正好的例子