c# – 获取订单和订单行计数子查询
作者:互联网
我正在将旧的App移植到Nhibernate.
旧的应用程序广泛使用ORACLE包,我想摆脱它.
我已经开始绘制几张桌子,事情看起来效果很好.
现在,我有这个查询,我希望能够通过QueryOver管理…或类似的东西:
SELECT
Orders.*
(SELECT COUNT(*) FROM OrderLines
WHERE OrderLines.CompanyCode = Orders.CompanyCode
AND OrderLines.OrderNumber = Orders.OrderNumber
AND NOT (OrderLines.OCLSCOM = 'Y' AND OrderLines.OCLSSEQ = 0)
AND OrderLines.Status = 'R') OrderLinesCount
FROM
Orders
WHERE
AND Orders.CompanyCode = [CompanyCode];
[CompanyCode]是一个过滤器.
我必须映射文件(Orders和OrderLines),我的关联看起来像这样:
<class name="Order" table="Orders">
...
<set name="OrderLines" access="field.pascalcase-underscore" inverse="true" lazy="extra" cascade="none">
<key>
<column name="OrderNumber" not-null="true"/>
<column name="CompanyCode" not-null="true"/>
</key>
<one-to-many class="OrderLine" not-found ="ignore"/>
</set>
</class>
我的订单表的主键是CompanyCode和OrderNumber.
我想查询订单并获取每个订单的行数.
我已经实现了我想要在Order映射上添加公式属性(感谢Ayende):
<property name="OrderLinesCount" formula="(SELECT COUNT(*) FROM OrderLines WHERE OrderLines.CompanyCode = CompanyCode AND OrderLines.OrderNumber = OrderNumber AND NOT (OrderLines.OCLSCOM = 'Y' AND OrderLines.OCLSSEQ = 0) AND OrderLines.Status = 'R')" />
但我害怕我的客户可能决定有一天改变那些讨厌的过滤器,我将被迫重新编译整个项目.
有没有办法用子查询(QueryOver)实现相同的结果?
在此先感谢您的帮助.
解决方法:
可能不是您正在寻找的,但您考虑过该集合的where子句.您可以调用order.OrderLinesFiltered.Count来获取值
<set name="OrderLinesFiltered" table="OrderLines"
access="field.pascalcase-underscore" inverse="true" lazy="extra"
cascade="none"
where=" NOT (OCLSCOM = 'Y' AND OCLSSEQ = 0) AND Status = 'R' ">
<key>
<column name="OrderNumber" not-null="true"/>
<column name="CompanyCode" not-null="true"/>
</key>
<one-to-many class="OrderLine" not-found ="ignore"/>
</set>
标签:c,subquery,nhibernate,queryover,nhibernate-queryover 来源: https://codeday.me/bug/20190710/1418811.html