I'd like to create DetachedCriteria which would perform something similar to the following SQL code:
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.
I'd appreciate any advice for this sample and also any advice on NHibernate materials on advanced DetachedCriteria querying.
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();