0

我需要选择有条件的行数:

查询以收集完整计数:

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
4

1 回答 1

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)
于 2012-09-17T16:09:28.023 回答