c# - 在 QueryOver 中

标签 c# nhibernate orm queryover

我们正在努力解决以下问题。我们选择的 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/

相关文章:

c# - 使用 NHibernate 插入带有复合键的记录

NHibernate动态更新无法更新拦截器中更改的数据

.net - 关于学习哪些.NET ORM的一些建议

c# - 单元测试时 HttpContext.Current 为空

c# - 这个 C# 语法可能吗?

如果这些参数是可选的,C# 不传递参数

c# - 使用 LINQ 插入数据库

nhibernate - 流畅的 NHibernate 和存储过程

c# - 从用户定义的类创建 SQL Server 表

python - 重写delete() 与使用预删除信号