QueryOver find alias string

I have the following aggregation request:

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>()); 

Since to calculate the average time it is required to call the datiff function for recording, I use SqlProjection

  private static AggregateProjection CountAverageCompletionTimeForTasksCriteria() { return Projections.Avg(Projections .SqlProjection("datediff(ss, Created, CompletedDate) as CompletionTimeInSeconds", new[] {"CompletionTimeInSeconds"}, new[] {NHibernateUtil.Double})); } 

The problem is that both Task and TaskChain have the same names for the Created and CompletedDate properties, so I need to pass an alias. But I can not get the string alias to form the correct sql projection. Is there a way to get these aliases, or can I insert in a function dated in the request in another way?

+4
source share
1 answer

NHibernate does not provide many complex options for working with aliases when using SqlProjection.

The only workaround is to profile the query first without a dated projection and see which NHibernate aliases are used for Task and TaskChain (they will probably be like this_ and * this_1 *). You can then manually write these aliases into the projection as follows:

 .SqlProjection("datediff(ss, this_.Created, this_.CompletedDate) as CompletionTimeInSeconds", new[] {"CompletionTimeInSeconds"}, new[] {NHibernateUtil.Double}) 

Obviously, this is far from ideal, but it will work.

0
source

All Articles