Nhibernate QueryOver - 带有谓词的.SelectCount()

时间:2012-09-17 15:30:04

标签: nhibernate queryover

我需要选择条件的行数:

查询收集完整计数:

var searchs = searchQuery.SelectList
                    (list => list

                                .SelectGroup(order => order.Id).WithAlias(() => groupResult.GlobalId)
                                .SelectCount(() => _transaction.ReturnStatus).WithAlias(() => groupResult.DeclineCount)
                    )

我需要等于201的交易数量。这样的事情:

.SelectCount(() => _transaction.ReturnStatus == 201).WithAlias(() => groupResult.DeclineCount) //runtime error

提前致谢!

PS: 原始SQL查询:

SELECT TOP 100
        globalOrd.ID AS GlobalId ,
        SUM(CASE WHEN transact.returnStatus = 201 THEN 1
                 ELSE 0
            END) AS DeclineCount
FROM    Orders.Global globalOrd
        INNER JOIN Orders.TransactionDetail transactDet ON globalOrd.ID = transactDet.DetailID
        INNER JOIN Orders.[Transaction] transact ON transactDet.TransactionID = transact.ID
GROUP BY globalOrd.ID

1 个答案:

答案 0 :(得分:0)

如果您不需要同一查询中的总计数,您只需在SelectList之前添加限制:

var searchs = searchQuery.SelectList
    (list => list
                .Where(() => _transaction.ReturnStatus == 201)
                .SelectGroup(order => order.Id).WithAlias(() => groupResult.GlobalId)
                .SelectCount(() => _transaction.ReturnStatus).WithAlias(() => groupResult.DeclineCount)
    )

但是,如果你想要总数和限制数,那么你必须使用SqlProjection来做后者:

 SUM(CASE {alias}.ReturnStatus WHEN 201 THEN 1 ELSE 0 END)