如何包含针对匹配记录计数的 nHibernate 标准
背景:
- 我有主表和明细表,M 和 D;
- M 包含订单,D 包含订单详细信息以及各种尺寸(S、M、L、XL)的零件。
- 给定订单可能包含任意数量的零件。
- 95% 的所有订单至少包含一件尺寸为 S 的商品
- 新要求是将来可以添加尺寸,因此对 SMLXL 列表进行硬编码不再有效
我正在寻找一个返回所有订单列表 M 的 nHibernate 查询仅包含特定尺寸的零件。
换句话说,返回仅包含部分尺寸 S 的所有订单,并排除包含混合尺寸的所有订单。
我正在使用:
matching_orders.Add(
Expression.Conjunction()
.Add(Subqueries.WhereProperty<Orders>(o => o.OrderId).In(DetailQueryOver(S)))
.Add(Subqueries.WhereProperty<Orders>(o => o.OrderId).NotIn(DetailQueryOver(M)))
.Add(Subqueries.WhereProperty<Orders>(o => o.OrderId).NotIn(DetailQueryOver(L)))
.Add(Subqueries.WhereProperty<Orders>(o => o.OrderId).NotIn(DetailQueryOver(XL)))
一定有更好的方法。像“where count(DISTINCT SIZES) = 1”之类的东西
,但我不确定如何在 nHibernate 中实现它。
建议?
BACKGROUND:
- I have Master and Detail tables, M and D;
- M contains orders and D contains order detail with parts of various sizes (S, M, L, XL)
- A given order might contain any number of parts.
- 95% of all orders contain at least one item of size S
- New requirement is that sizes can be added in the future, so hard coding the list for SMLXL no longer works
I am looking for a nHibernate query that returns a list of all orders M that only contain parts with a specific size.
In other words, return all order containing only parts of size S, and exclude all orders that contain a mixture of sizes.
I was using:
matching_orders.Add(
Expression.Conjunction()
.Add(Subqueries.WhereProperty<Orders>(o => o.OrderId).In(DetailQueryOver(S)))
.Add(Subqueries.WhereProperty<Orders>(o => o.OrderId).NotIn(DetailQueryOver(M)))
.Add(Subqueries.WhereProperty<Orders>(o => o.OrderId).NotIn(DetailQueryOver(L)))
.Add(Subqueries.WhereProperty<Orders>(o => o.OrderId).NotIn(DetailQueryOver(XL)))
There must be a better way. Something like "where count(DISTINCT SIZES) = 1"
But I'm not sure how to implement this in nHibernate.
Suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 dotjoe 所建议的,我相信
having
子句是通过将投影存储在临时变量中,然后在投影列表和限制列表中使用它来完成的,尽管我只使用做到了这一点ICriteria
查询,而不是QueryOver
。编写此查询的另一种方法是使用两个子查询 - 一个表示您要查找的大小,另一个表示所有其他大小。类似...
我们可以进一步将此答案转换为
QueryOver
查询,但我不想破坏您的乐趣。这足以让您指明正确的方向吗?As dotjoe suggested, I believe
having
clauses are accomplished by stashing a projection in a temporary variable, then using it in both the projection list and the restriction list, though I've only done that withICriteria
queries, notQueryOver
.Another way to write this query is to use two subqueries - one to represent the size you are looking for, another to represent all the other sizes. Something like...
We could take this answer another step further and translate this into a
QueryOver
query, but I don't want to spoil your fun. Is that enough to get you pointed in the right direction?