我有下一个聚合查询:
IQueryOver<CustomerTask, CustomerTask> query = GetSession().QueryOver(() => task)
.JoinAlias(() => task.TaskList, () => taskList)
.JoinAlias(() => task.TaskChain, () => taskChain)
.Where(() => taskList.SalesFlow == salesFlow)
.Select(
Projections.Group(() => taskList.Id)
.WithAlias(() => salesFlowTaskStatisticsGridRow.RowId),
Projections.Group(() => taskList.SalesFlow.Id)
.WithAlias(() => salesFlowTaskStatisticsGridRow.SalesFlowId),
Projections.Group(() => taskList.Name).WithAlias(() => salesFlowTaskStatisticsGridRow.Name),
// ReSharper disable ExpressionIsAlwaysNull
criteriaBuilder.FindAverageCompletionTime(salesFlowTaskStatisticsGridRow)
// ReSharper restore ExpressionIsAlwaysNull
).TransformUsing(Transformers.AliasToBean<SalesFlowTaskStatisticsGridRow>());
由于计算平均时间需要调用 datediff 函数来编写它,所以我使用 SqlProjection
private static AggregateProjection CountAverageCompletionTimeForTasksCriteria()
{
return Projections.Avg(Projections
.SqlProjection("datediff(ss, Created, CompletedDate) as CompletionTimeInSeconds",
new[] {"CompletionTimeInSeconds"}, new[] {NHibernateUtil.Double}));
}
问题是Task和TaskChain对于Created和CompletedDate属性具有相同的名称,所以我需要传递一个别名。但我无法设法让字符串别名形成正确的 sql 投影。有没有办法获得这些别名,或者我可以通过其他方式将datediff函数插入查询中吗?