【发布时间】:2011-12-06 05:55:18
【问题描述】:
我正在努力将以下(简化的)HQL 转换为 QueryOver:
select subscription
from Subscription as subscription
where not exists (
from Shipment as shipment
where shipment.Subscription = subscription
and (shipment.DeliveryDate = :deliveryDate)
)
我已经走到这一步了:
Subscription subscription = null;
Session.QueryOver(() => subscription)
.Where(Subqueries.NotExists(QueryOver.Of<Shipment>()
.Where(shipment => shipment.Subscription == subscription)
.And(shipment=> shipment.DeliveryDate == deliveryDate)
.Select(shipment => shipment.Id).DetachedCriteria));
.TransformUsing(new DistinctRootEntityResultTransformer());
问题是上面的Subqueries 和Where 语句给了我以下(无效的)where 子句:
where shipment.SubscriptionId is null
当我想要的是:
where shipment.SubscriptionId = subscription.Id
因此在构造 SQL 时不考虑别名及其行级值,而是使用其初始值null 与Shipment 的SubscriptionId 进行比较。
更新
使用 dotjoe 提供的解决方案,我能够编写如下 QueryOver 语句:
Subscription subscription = null;
Session.QueryOver(() => subscription)
.WithSubquery.WhereNotExists(QueryOver.Of<Shipment>()
.Where(shipment => shipment.Subscription.Id == subscription.Id)
.And(shipment => shipment.DeliveryDate == deliveryDate)
.Select(shipment => shipment.Id));
【问题讨论】:
标签: c# nhibernate hql subquery queryover