NHibernate的QueryOver与子查询和别名 [英] NHibernate QueryOver with sub-query and alias

查看:160
本文介绍了NHibernate的QueryOver与子查询和别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我竭力以下(简体)HQL转换为QueryOver:

 选择订阅$从认购B $ b作为认购
,其中不存在(自装运货物为
,其中shipment.Subscription =认购
和(shipment.DeliveryDate =
:deliveryDate)

我已经走到这一步:

 订阅订阅= NULL; 

Session.QueryOver(()=>认购)
。凡(Subqueries.NotExists(QueryOver.Of<装运>()
。凡(装运= GT;装运.Subscription ==认购)
。而(装运= GT; shipment.DeliveryDate == deliveryDate)
。选择(装运= GT; shipment.Id).DetachedCriteria));
.TransformUsing(新DistinctRootEntityResultTransformer());



现在的问题是,上述子查询其中,语句给我下面的(无效)where子句:

 其中, shipment.SubscriptionId为null 

当我想要的是:

 其中shipment.SubscriptionId = subscription.Id 

所以构建,而不是它的初始值用来比较的装运的 SubscriptionId



更新



通过dotjoe的提供解决方案,我能写的 QueryOver 声明如下:

 订阅订阅= NULL; 

Session.QueryOver(()=>认购)
.WithSubquery.WhereNotExists(QueryOver.Of<装运>()
。凡(装运= GT; shipment.Subscription .ID == subscription.Id)
。而(装运= GT; shipment.DeliveryDate == deliveryDate)
。选择(装运= GT; shipment.Id));


解决方案

尝试

 。凡(装运= GT; shipment.Subscription.Id == subscription.Id)


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

解决方案

try

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

这篇关于NHibernate的QueryOver与子查询和别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆