【发布时间】:2014-05-21 01:32:47
【问题描述】:
我有以下 NHibernate QueryOver 查询:
var query = session.QueryOver<IssuanceReportLogEntity>()
.Where(i => i.CustomerId == customer.Id && i.RollbackIssuanceId == null);
if (Parms.StartDate != null) query.Where(i => i.IssuanceDateCreated >= Parms.StartDate);
if (Parms.EndDate != null) query.Where(i => i.IssuanceDateCreated <= Parms.EndDate);
if (Parms.GroupId != null) query.Where(i => i.RecipientGroupId == Parms.GroupId);
if (Parms.ProgramId != null) query.Where(i => i.ProgramId == Parms.ProgramId);
query.Select(
Projections.Group<IssuanceReportLogEntity>(x => x.RecipientGroupId).WithAlias(() => receiver.RecipientGroupId),
Projections.Group<IssuanceReportLogEntity>(x => x.RecipientId).WithAlias(() => receiver.RecipientId),
Projections.Group<IssuanceReportLogEntity>(x => x.RecipientFullName).WithAlias(() => receiver.RecipientFullName),
Projections.Group<IssuanceReportLogEntity>(x => x.RecipientEmployeeNumber).WithAlias(() => receiver.RecipientEmployeeNumber),
Projections.Group<IssuanceReportLogEntity>(x => x.RecipientTitle).WithAlias(() => receiver.RecipientTitle),
Projections.Count<IssuanceReportLogEntity>(x=>x.RecipientGroupId).WithAlias(()=>receiver.RecognitionTotalReceived),
Projections.Sum<IssuanceReportLogEntity>(x=>x.Points).WithAlias(()=>receiver.TotalPoints));
if (customer.Settings.PointsEnabled)
{
query.OrderBy(Projections.Sum<IssuanceReportLogEntity>(x => x.Points)).Desc();
}
else
{
query.OrderBy(Projections.Count<IssuanceReportLogEntity>(x => x.InitiatorId)).Desc();
}
query.TransformUsing(Transformers.AliasToBean<TopReceiver>());
这会生成以下查询(对于数据的选择是正确的):
SELECT TOP (20 /* @p0 */) this_.RecipientGroupId as y0_,
this_.RecipientId as y1_,
this_.RecipientFullName as y2_,
this_.RecipientEmployeeNumber as y3_,
this_.RecipientTitle as y4_,
count(this_.RecipientGroupId) as y5_,
sum(this_.Points) as y6_
FROM [IssuanceReportLog] this_
WHERE (this_.CustomerId = '30a678bc-264a-4a04-aac4-a3270158929f' /* @p1 */
and this_.RollbackIssuanceId is null)
and this_.RecipientGroupId = '2fd9ec20-e870-42f6-b345-a3270158992a' /* @p2 */
GROUP BY this_.RecipientGroupId,
this_.RecipientId,
this_.RecipientFullName,
this_.RecipientEmployeeNumber,
this_.RecipientTitle
ORDER BY sum(this_.Points) desc
我需要做的是弄清楚如何让 NHibernate 在不删除 Group By 的情况下生成行计数,本质上是做类似的事情(注意前面的查询本质上是一个没有 TOP 的子查询):
SELECT COUNT(*) FROM (
SELECT this_.RecipientGroupId as y0_,
this_.RecipientId as y1_,
this_.RecipientFullName as y2_,
this_.RecipientEmployeeNumber as y3_,
this_.RecipientTitle as y4_,
count(this_.RecipientGroupId) as y5_,
sum(this_.Points) as y6_
FROM [IssuanceReportLog] this_
WHERE (this_.CustomerId = '30a678bc-264a-4a04-aac4-a3270158929f' /* @p1 */
and this_.RollbackIssuanceId is null)
and this_.RecipientGroupId = '2fd9ec20-e870-42f6-b345-a3270158992a' /* @p2 */
GROUP BY this_.RecipientGroupId,
this_.RecipientId,
this_.RecipientFullName,
this_.RecipientEmployeeNumber,
this_.RecipientTitle
) AS Query
每次我尝试让行计数起作用时,NH 都会删除 GROUP BY。上面的 SQL 按我的预期工作。
关于如何让 NHibernate 吐出该 SQL 的任何想法?
【问题讨论】:
标签: c# sql sql-server nhibernate