NHibernate QueryOver with sub-query and alias

白昼怎懂夜的黑 提交于 2019-12-12 09:44:07

问题


I'm struggling to convert the following (simplified) HQL to 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:

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.

Update

With dotjoe's provided solution, I was able to write the QueryOver statement as follows:

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

回答1:


try

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


来源:https://stackoverflow.com/questions/7742453/nhibernate-queryover-with-sub-query-and-alias

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!