简体   繁体   English

NHibernate DetachedCriteria-如何在外部联接集上使用类似于Oracle的NVL之类的东西

[英]NHibernate DetachedCriteria - how to use something like Oracle's NVL on outer-joined set

I'd like to create DetachedCriteria which would perform something similar to the following SQL code: 我想创建DetachedCriteria,它将执行类似于以下SQL代码的操作:

select *
FROM PRICELIST pl
LEFT OUTER JOIN PRICELISTDURATIONSHIFT sh ON sh.PRICELISTID = pl.ID
WHERE sh.CUSTOMERID = :cust
  AND nvl(sh.DATEFROM, pl.DATEFROM) <= :dt
ORDER BY nvl(sh.DATEFROM, pl.DATEFROM) DESC

I'm able to left-outer-join the PriceListDurationShift class/table and add the Customer constraint, but I'm not able to figure out how to add something like the nvl(sh.DATEFROM, pl.DATEFROM) <= :dt restriction. 我可以从左到外加入PriceListDurationShift类/表并添加Customer约束,但是我不知道如何添加类似nvl(sh.DATEFROM,pl.DATEFROM)<=:dt的内容限制。

I'd appreciate any advice for this sample and also any advice on NHibernate materials on advanced DetachedCriteria querying. 我希望您能对此示例提供任何建议,也希望获得有关NHibernate材料上有关高级DetachedCriteria查询的建议。


hopefully this would work: 希望这会工作:

var priceLists = priceListRepo.FindAll(DetachedCriteria.For<PriceList>("pl")
    .CreateCriteria((PriceList x) => x.PriceListDurationShifts, () => shift,
        JoinType.LeftOuterJoin)
    .Add<PriceListDurationShift>(x => x.Customer == cust)
    .Add(Expression.Or(
        Expression.And(Restrictions.IsNull("shift.DateFrom"),
            Restrictions.Le("pl.DateFrom", dt.DateTo)),
        Expression.And(Restrictions.IsNotNull("shift.DateFrom"),
            Restrictions.Le("shift.DateFrom", dt.DateTo))
        ))
    )
    .ToList();

I've looked for something like this as well, but haven't found a solution to it. 我也在寻找类似的东西,但是还没有找到解决方案。

In the meantime, I've solved this by using HQL instead of the Criteria API. 同时,我已经通过使用HQL而不是Criteria API解决了这一问题。

HQL has a method coalesce which can be used for this purpose. HQL具有可用于此目的的方法coalesce

from PriceList as pl
left join pl.PriceListDurationShift as shift
where coalesce (shift.DateFrom, pl.DateFrom) <= :dt
order by coalesce (shift.DateFrom, pl.DateFrom) 

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

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