NHibernate QueryOver带有子查询和别名

时间:2011-10-12 15:23:04

标签: c# nhibernate hql subquery queryover

我正在努力将以下(简化)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());

问题是上面的SubqueriesWhere语句给了我以下(无效)where子句:

where shipment.SubscriptionId is null

当我想要的是:

where shipment.SubscriptionId = subscription.Id

因此,在构造SQL时不会考虑别名及其行级别值,而是使用其初始值nullShipment的{​​{1}}进行比较

更新

使用dotjoe提供的解决方案,我能够编写SubscriptionId语句,如下所示:

QueryOver

1 个答案:

答案 0 :(得分:4)

.Where(shipment => shipment.Subscription.Id == subscription.Id)
相关问题