Я пытаюсь заставить avg работать на временные рамки в linq для NHibernate.
Я добавил реестр:
public class CustomLinqToHqlGeneratorsRegistry : DefaultLinqToHqlGeneratorsRegistry
{
public CustomLinqToHqlGeneratorsRegistry()
{
this.Merge(new AvgTimeSpanGenerator());
}
}
Генератор:
public class AvgTimeSpanGenerator : BaseHqlGeneratorForMethod
{
public AvgTimeSpanGenerator()
{
SupportedMethods = new[]
{
NHibernate.Linq.ReflectionHelper.GetMethodDefinition<IEnumerable<TimeSpan>>(x => x.Avg())
};
}
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
return treeBuilder.MethodCall("Avg", visitor.Visit(targetObject).AsExpression());
}
}
Пожалуйста, обратите внимание, что моя BuildHql
реализация может быть неправильной (на самом деле я ее не особо продумал), но я установил там точку останова, и она никогда не была достигнута.
И метод:
public static class NHibernateLinqExtension
{
public static TimeSpan Avg(this IEnumerable<TimeSpan> timeSpan)
{
return new TimeSpan((long)timeSpan.Select(x => x.Ticks).Average());
}
}
Я также зарегистрировал реестр в NHibernate:
configuration.LinqToHqlGeneratorsRegistry<CustomLinqToHqlGeneratorsRegistry>();
Но когда я выполняю запрос:
var data = (from tcdr in uow.UnitOfWorkSession.Query<TCDR>()
group tcdr.Duration by tcdr.Name into g
select new
{
MaxDuration = g.Max(),
MinDuration = g.Min(),
AvgDuration = g.Avg()
}).ToList();
Выдает InvalidOperationException Code supposed to be unreachable
Любойключ?