Nir*_*uka 4 c# nhibernate queryover
我有两个表Invoice和InvoiceLineItem.该InvoiceLineItem表包含:
Id
InvoiceId
Quantity
UnitPrice
Run Code Online (Sandbox Code Playgroud)
列.我想创建NHibernate QueryOver语句来对发票行项目进行分组,InvoiceId并得到和的乘积之UnitPrice和Quantity
SQL就是这样.
SELECT InvoiceId, SUM(Quantity*UnitPrice) AS Total
FROM InvoiceLineItem
GROUP BY InvoiceId
Run Code Online (Sandbox Code Playgroud)
我用过,Projections.Sum但我不确定我们如何在其中增加两列(如果这是正确的方法).
And*_*ker 10
它看起来并不是一个很好的方法.建立这个答案,你可以使用VarArgsSQLFunction:
InvoiceLineItem lineItemAlias = null;
session.QueryOver<InvoiceLineItem>(() => lineItemAlias)
.SelectList(list => list
.Select(Projections.Sum(
Projections.SqlFunction(new VarArgsSQLFunction("(", "*", ")"),
NHibernateUtil.Double,
Projections.Property(() => lineItemAlias.Quantity),
Projections.Property(() => lineItemAlias.UnitPrice))))
.SelectGroup(() => lineItemAlias.Invoice.Id)
// TransformUsing, List<>, etc.
Run Code Online (Sandbox Code Playgroud)
这将生成如下所示的SQL:
SELECT
sum((this_.Quantity*this_.UnitPrice)) as y0_,
this_.InvoiceId as y1_
FROM
InvoiceLineItem this_
GROUP BY
this_.InvoiceId
Run Code Online (Sandbox Code Playgroud)