我写了这个SQL查询:
SELECT R.Name, R.PointsNeeded, A.EmailAddress, SUM(PH.Points) AS TotalPoints
FROM Business AS B
INNER JOIN Reward AS R ON R.BusinessId = B.Id
INNER JOIN Account AS A ON A.BusinessId = B.Id
INNER JOIN PointHistory PH ON PH.AccountId = A.Id
WHERE B.Serial = 'F5C17337-F675-4270-8A57-FE2F5782B5CB'
GROUP BY R.Name, A.EmailAddress, R.PointsNeeded
HAVING SUM(PH.Points) >= R.PointsNeeded
ORDER BY A.EmailAddress ASC
返回3个结果。我想用linq / fluent api创建它。我无法弄清楚如何做HAVING
所以我创建了这个:
var eligableAccounts = _context.Business
.Join(_context.Reward, B => B.Id, R => R.BusinessId, (Business, Rewards) => new { Business, Rewards })
.Join(_context.Account, BusinessRewards => BusinessRewards.Business.Id, A => A.BusinessId, (BusinessRewards, Account) => new
{
Business = BusinessRewards.Business,
Rewards = BusinessRewards.Rewards,
Account = Account
})
.Join(_context.PointHistory, Model => Model.Account.Id, PointHistory => PointHistory.AccountId, (Model, PointHistory) => new
{
Business = Model.Business,
Rewards = Model.Rewards,
Account = Model.Account,
PointHistory = PointHistory
})
.Where(Model => Model.Business.Serial == businessSerial)
.GroupBy(Model => new { Model.Rewards.Name, Model.Account.EmailAddress, Model.Rewards.PointsNeeded })
.Where(Group => Group.Sum(Model => Model.PointHistory.Points) >= Group.Key.PointsNeeded)
.Select(Group => new {
TotalPoints = Group.Sum(Model => Model.PointHistory.Points),
EmailAddress = Group.Key.EmailAddress,
RewardName = Group.Key.Name,
PointsNeeded = Group.Key.PointsNeeded
}).ToList();
这有效,eligableAccounts
包含与SQL Management Studio中直接执行时我的SQL查询返回的相同3个结果。我希望看到幕后发生了什么,令我惊讶的是,每个结果集都会被返回,然后.net正在做一些神奇的调整,只能获得我需要的3条记录。以下是EF实际在数据库上执行的内容:
exec sp_executesql N'SELECT [B].[Id], [B].[Code], [B].[LastProcessedDateTime], [B].[Name], [B].[PointRatio], [B].[Serial], [Rewards].[Id], [Rewards].[BusinessId], [Rewards].[Name], [Rewards].[PointsNeeded], [Account].[Id], [Account].[BusinessId], [Account].[City], [Account].[Created], [Account].[EmailAddress], [Account].[EmailSent], [Account].[FirstName], [Account].[LastName], [Account].[Password], [Account].[PasswordSalt], [Account].[Phone], [Account].[RoleId], [Account].[State], [Account].[Updated], [Account].[VerificationCode], [Account].[Verified], [Account].[ZipCode], [PointHistory].[Id], [PointHistory].[AccountId], [PointHistory].[Points], [PointHistory].[TransactionDate], [PointHistory].[TransactionType]
FROM [Business] AS [B]
INNER JOIN [Reward] AS [Rewards] ON [B].[Id] = [Rewards].[BusinessId]
INNER JOIN [Account] AS [Account] ON [B].[Id] = [Account].[BusinessId]
INNER JOIN [PointHistory] AS [PointHistory] ON [Account].[Id] = [PointHistory].[AccountId]
WHERE [B].[Serial] = @__businessSerial_0
ORDER BY [Rewards].[Name], [Account].[EmailAddress], [Rewards].[PointsNeeded]',N'@__businessSerial_0 nvarchar(4000)',@__businessSerial_0=N'F5C17337-F675-4270-8A57-FE2F5782B5CB'
直接在数据库上执行时,此SQL将返回所有结果,因为EF未创建HAVING
语句。有谁知道如何编写我的EF代码来生成执行的正确查询?这可能会开始返回数十万条记录,我不喜欢.net处理减少内存中结果集的想法。我宁愿把它推到数据库。如果我必须创建一个存储过程,我可以这样做,但不是真的想要。任何帮助表示赞赏。谢谢!
答案 0 :(得分:0)
虽然我无法修复EF Core的限制(除非您想切换到完整的EF),但可以将SQL转换为LINQ。我发现使用查询语法更容易进行分组:
var ans = from b in _context.Business
join r in _context.Reward on b.Id equals r.BusinessId
join a in _context.Account on b.Id equals a.BusinessId
join ph in _context.PointHistory on a.Id equals ph.AccountId
where b.Serial == "F5C17337-F675-4270-8A57-FE2F5782B5CB"
group new { b, r, a, ph } by new { r.Name, a.EmailAddress, r.PointsNeeded } into grp
let TotalPoints = grp.Sum(g => g.ph.Points)
where TotalPoints >= grp.Key.PointsNeeded
orderby grp.Key.EmailAddress
select new { grp.Key.Name, grp.Key.PointsNeeded, grp.Key.EmailAddress, TotalPoints };
请注意,翻译是直截了当的,几乎是机械的。
使用lambda语法的翻译对我来说很简单。