C# 有疑问

C# 有疑问,c#,nhibernate,orm,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

我们正在努力解决以下问题。。我们选择的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
从SellerProfile sp中选择sp.Id、SUM(p.PriceAmount)、SUM(i.BruttoAmount)
左连接SellerProfile\u spi上的发票spi.SellerProfile=sp.Id
spi上的左连接发票i。发票=i.Id
左加入SellerProfile\u spp上的付款spp.SellerProfile=sp.Id
左连接付款p在spp上付款=p.Id
其中i.付款日期<'2011-07-12'
按sp.Id分组
有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>();
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
发票=空;
付款=空;
SellerProfile seller=空;
var sellerIds=Session.QueryOver(()=>seller)
.Left.JoinQueryOver(()=>卖方付款,()=>付款)
.Left.JoinQueryOver(()=>卖方发票,()=>发票)
.Where(()=>invoice.PaymentDateseller.Id))
其中(Restrictions.Lt(新算术运算符projection(“-”,NHibernateUtil.Decimal,Projections.Sum(()=>payment.Price.Amount)),Projections.Sum(()=>invoice.Brutto.Amount)),0)).List();
生成的SQL如下所示:

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>();
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
将此\uu.Id选择为y0\u
从SellerProfile this_uu内部加入资源所有者this_1_uu在此u.Id=此1_uu.Id
内部联接资源this_2_u在此_u.Id上=this_2_u.Id
左外联接SellerProfile\u付款付款付款4\u在此\uu.Id=payments4\uSellerProfile上
左外连接付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款=付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款付款
左外部联接SellerProfile\u发票发票6\u在此\uu.Id=发票6\uuSellerProfile
发票6上的左外部联接发票发票1。发票=发票1。Id
其中发票1付款日期<@p0
和(金额(付款2价格金额)-金额(发票1金额))<@p1
按此\u.Id分组
但是它抛出了一个异常,因为它将
子句放在
的第一行,而不是
的最后一行,我们的SQL不起作用


有什么帮助吗?谢谢…

如果查询版本、LINQ和标准API不支持子句逻辑(NH3.1)

不过,您可以使用HQL


哪个数据库以及生成的查询是什么?数据库是SQL Server。。。看看我的帖子,我已经添加了生成的SQL。。。