我正在尝试为 NHibernate 的 linq 中的时间跨度工作制作平均值。
我添加了注册表:
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
有什么线索吗?
慕后森
相关分类