gpt4 book ai didi

c# - 在 QueryOver 中

转载 作者:行者123 更新时间:2023-11-30 17:17:25 26 4
gpt4 key购买 nike

我们正在努力解决以下问题。我们选择的 ORM 解决方案是 NHibernate,我们想使用 QueryOver 样式编写查询。现在有一个新的难题需要解决,我们想进行如下查询:

select sp.Id, SUM(p.PriceAmount), SUM(i.BruttoAmount)  from SellerProfile sp
left join SellerProfile_Invoice spi on spi.SellerProfile = sp.Id
left join Invoice i on spi.Invoice = i.Id
left join SellerProfile_Payment spp on spp.SellerProfile = sp.Id
left join Payment p on spp.Payment = p.Id
where i.PaymentDate < '2011-07-12'
group by sp.Id
having SUM(ISNULL(p.PriceAmount,0)) - SUM(ISNULL(i.BruttoAmount,0)) < 0

所以我们写了这样的代码:

Invoice invoice = null;
Payment payment = null;
SellerProfile seller = null;

var sellerIds = Session.QueryOver<SellerProfile>(() => seller)
.Left.JoinQueryOver(() => seller.Payments, () => payment)
.Left.JoinQueryOver(() => seller.Invoices, () => invoice)
.Where(() => invoice.PaymentDate < DateTime.Now - timeSpan)
.Select(Projections.Group(() => seller.Id))
.Where(Restrictions.Lt(new ArithmeticOperatorProjection("-", NHibernateUtil.Decimal, Projections.Sum(() => payment.Price.Amount), Projections.Sum(() => invoice.Brutto.Amount)), 0)).List<int>();

生成的 SQL 如下所示:

SELECT this_.Id as y0_ 
FROM SellerProfile this_ inner join ResourceOwner this_1_ on this_.Id=this_1_.Id
inner join Resource this_2_ on this_.Id=this_2_.Id
left outer join SellerProfile_Payment payments4_ on this_.Id=payments4_.SellerProfile
left outer join Payment payment2_ on payments4_.Payment=payment2_.Id
left outer join SellerProfile_Invoice invoices6_ on this_.Id=invoices6_.SellerProfile
left outer join Invoice invoice1_ on invoices6_.Invoice=invoice1_.Id
WHERE invoice1_.PaymentDate < @p0
and (sum(payment2_.PriceAmount) - sum(invoice1_.BruttoAmount)) < @p1
GROUP BY this_.Id

但它会抛出异常,因为它将 and 子句放在第一个 where 而不是 having 放在最后一行,而我们的 SQL 不会工作...

有什么帮助吗?谢谢...

最佳答案

AFAIK QueryOver、LINQ 和 Criteria API 不支持 HAVING 子句逻辑 (NH 3.1)

不过您可以使用 HQL。

HQL Examples

关于c# - 在 QueryOver 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6477099/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com