简体   繁体   English

NHibernate QueryOver带有子查询和别名

[英]NHibernate QueryOver with sub-query and alias

I'm struggling to convert the following (simplified) HQL to QueryOver: 我正在努力将以下(简化)HQL转换为QueryOver:

select subscription
from Subscription as subscription
where not exists (
    from Shipment as shipment
    where shipment.Subscription = subscription
    and (shipment.DeliveryDate  = :deliveryDate)
)

I've come this far: 我走到这一步:

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());

The problem is that the above Subqueries and Where statement gives me the following (invalid) where clause: 问题是上面的SubqueriesWhere语句给了我以下(无效)where子句:

where shipment.SubscriptionId is null

when what I want is: 当我想要的是:

where shipment.SubscriptionId = subscription.Id

So the alias and its row-level value isn't taken into account when constructing the SQL, instead its initial value null is used to compare to the Shipment 's SubscriptionId . 因此,在构造SQL时不考虑别名及其行级值,而是将其初始值null用于与ShipmentSubscriptionId进行比较。

Update 更新

With dotjoe's provided solution, I was able to write the QueryOver statement as follows: 使用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));

尝试

.Where(shipment => shipment.Subscription.Id == subscription.Id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM