簡體   English   中英

NHibernate QueryOver帶有子查詢和別名

[英]NHibernate QueryOver with sub-query and alias

我正在努力將以下(簡化)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時不考慮別名及其行級值,而是將其初始值null用於與ShipmentSubscriptionId進行比較。

更新

使用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