我无法将以下内容转换为Sql到nhibernate:
SELECT DISTINCT Booking.*
FROM Booking WHERE Booking.Status = 2
OR EXISTS (SELECT 1
FROM JourneyFuture
WHERE JourneyFuture.BookingId = Booking.BookingId
AND ((JourneyFuture.[IsDriverAssigned] = 0
AND JourneyFuture.[Status] = 1) -- live
OR JourneyFuture.[Status] = 2
OR JourneyFuture.[Status] = 4
OR (JourneyFuture.[Status] = 1
AND (JourneyFuture.IsMetricCalculated = 0
OR JourneyFuture.Duration = 0
OR JourneyFuture.Distance = 0))))
到目前为止,这是我对Nhibernate的看法:
Booking bookingAlias = null;
var journeyFuture = QueryOver.Of<JourneyFuture>().Where(x=> x.Booking.Id==bookingAlias.Id).
And(x => (!x.IsDriverAssigned && x.Status==JourneyStatusType.Live) || x.Status==JourneyStatusType.CancelRequested
|| x.Status == JourneyStatusType.Modified ||
(x.Status == JourneyStatusType.Live && (!x.IsMetricCalculated || x.Duration==0 || x.Distance==0)))
.Select(x=> x.Booking);
var result = session.QueryOver<Booking>(() => bookingAlias)
.Where(x => x.Status == BookingStatusType.CancelRequested)
.WithSubquery.WhereExists(journeyFuture)
.List<Booking>();
然而,这产生了一个“和”存在是有道理的,但我如何获得OR EXISTS?
任何帮助表示感谢。
答案 0 :(得分:2)
以下内容应该有效:
Booking bookingAlias = null;
var journeyFuture = QueryOver.Of<JourneyFuture>().Where(x=> x.Booking.Id==bookingAlias.Id).
And(x => (!x.IsDriverAssigned && x.Status==JourneyStatusType.Live) || x.Status==JourneyStatusType.CancelRequested
|| x.Status == JourneyStatusType.Modified ||
(x.Status == JourneyStatusType.Live && (!x.IsMetricCalculated || x.Duration==0 || x.Distance==0)))
.Select(x=> x.Booking);
var result = session.QueryOver<Booking>(() => bookingAlias)
.Where(
Restrictions.Or(
Restrictions.Where(() => bookingAlias.Status == BookingStatusType.CancelRequested),
Subqueries.Exists(journeyFuture.DetachedCriteria)))
.List<Booking>();
基本上用更强大的.WithSubquery
和Restrictions.Or
替换Subqueries.Exists
。我希望有一种方法可以用.WithSubquery
来做到这一点,但我不确定是否存在。
一般而言,通过复杂的限制,您可以深入了解Restrictions
课程。这样做的缺点是它通常会使您的代码更复杂。