Nhibernate Criterias内连接子查询

时间:2012-03-14 12:58:08

标签: sql fluent-nhibernate subquery icriteria nhibernate-criteria

我试图在Nhibernate ICriteria中复制以下sql

    SELECT DISTINCT AP.ID
FROM ActivityParts AP
    INNER JOIN ActivityBookings AB ON AB.ActivityPartID = AP.ID
    INNER JOIN OrderPartBookings OPB ON OPB.ActivityBookingID = AB.ID
    INNER JOIN OrderParts OP ON OP.ID = OPB.OrderPartID
    INNER JOIN Orders O ON O.ID = OP.OrderID
    LEFT JOIN Invoices I ON I.ID = (SELECT TOP 1 ID FROM Invoices WHERE OrderReferenceID = OP.ID AND Cancelled = 0 ORDER BY Created DESC)
WHERE
    O.OrderStatus != 'Cancelled'
    AND OP.Payed = 0
    AND (I.ID IS NOT NULL AND DATEADD(Day,1, I.DueDate) < @date)

到目前为止,我已设法获得以下内容(注意:我没有对其进行分析,所以我不太确定sql在标准中的相似程度如何):

var invoiceSubCrit = DetachedCriteria.For<Invoice>()
            .Add(Restrictions.Eq("Cancelled", false))
            .Add(Restrictions.EqProperty("OrderReference", "OP.ID"))
            .AddOrder(NhOrder.Desc("Created"))
            .SetProjection(Projections.Property("DueDate"))
            .SetMaxResults(1);

        var crit = Session.CreateCriteria<OrderPart>("OP")
            .CreateCriteria("OP.Bookings", "AB", JoinType.InnerJoin)
            .CreateCriteria("OP.Order", "O", JoinType.InnerJoin)
            .CreateCriteria("AB.ActivityPart", "AP", JoinType.InnerJoin)
            .Add(NhExpression.Eq("AP.ID", ActivityID))
            .Add(NhExpression.Eq("OP.Payed", false))
            .Add(NhExpression.Not(NhExpression.Eq("O.OrderStatus", OrderStatus.Cancelled)))
            .Add(NhExpression.Not(NhExpression.Eq("AB.Status", QueueStatus.Reserve)))
            .Add(Subqueries.Lt(DateTime.Now.Date.AddDays(1), invoiceSubCrit))
            .SetResultTransformer(new DistinctRootEntityResultTransformer());

Uppdate: 得到了一些新的信息,并重新调整了sql。 Uppdate:有一个半工作子查询,需要弄清楚如何正确设置它。

2 个答案:

答案 0 :(得分:5)

最后让它工作,并在我的案例中使用最终工作版本更新了我的标准代码。我不确切知道这与我原来的SQL代码有多接近,但结果是最重要的,但最重要的是。

var invoiceSubCrit = DetachedCriteria.For<Invoice>()
            .Add(Restrictions.Eq("Cancelled", false))
            .Add(Restrictions.EqProperty("OrderReference.ID", "OP.ID"))
            .AddOrder(NhOrder.Desc("Created"))
            .SetProjection(Projections.Property("DueDate"))
            .SetMaxResults(1);

        var crit = Session.CreateCriteria<OrderPart>("OP")
            .CreateCriteria("OP.Bookings", "AB", JoinType.InnerJoin)
            .CreateCriteria("OP.Order", "O", JoinType.InnerJoin)
            .CreateCriteria("AB.ActivityPart", "AP", JoinType.InnerJoin)
            .Add(NhExpression.Eq("AP.ID", ActivityID))
            .Add(NhExpression.Eq("OP.Payed", false))
            .Add(NhExpression.Not(NhExpression.Eq("O.OrderStatus", OrderStatus.Cancelled)))
            .Add(NhExpression.Not(NhExpression.Eq("AB.Status", QueueStatus.Reserve)))
            .Add(Subqueries.Gt(DateTime.Now.Date, invoiceSubCrit))
            .SetResultTransformer(new DistinctRootEntityResultTransformer());

答案 1 :(得分:1)

您不能在from子句中使用带有选择的条件。尝试使用hql或过滤客户端

更新

.Add(Restrictions.EqProperty("OrderReference.ID", "OP.ID"))
// or 
.Add(Restrictions.EqProperty("OrderReference", "OP"))