Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为平均时间跨度生成NHibernate中的linq方法_C#_Linq_Nhibernate_Hql_Linq To Nhibernate - Fatal编程技术网

C# 为平均时间跨度生成NHibernate中的linq方法

C# 为平均时间跨度生成NHibernate中的linq方法,c#,linq,nhibernate,hql,linq-to-nhibernate,C#,Linq,Nhibernate,Hql,Linq To Nhibernate,我试图让timespan的平均值在linq为NHibernate工作 我添加了注册表: public class CustomLinqToHqlGeneratorsRegistry : DefaultLinqToHqlGeneratorsRegistry { public CustomLinqToHqlGeneratorsRegistry() { this.Merge(new AvgTimeSpanGenerator()); } } 发电机: publi

我试图让timespan的平均值在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());
    }
}
我还在NHibernate注册了注册处:

configuration.LinqToHqlGeneratorsRegistry<CustomLinqToHqlGeneratorsRegistry>();
configuration.LinqToHqlGeneratorsRegistry();
但当我执行查询时:

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();
var data=(来自uow.UnitOfWorkSession.Query()中的tcdr)
按tcdr.Name将tcdr.Duration分组为g
选择新的
{
MaxDuration=g.Max(),
MinDuration=g.Min(),
AvgDuration=g.Avg()
}).ToList();
它抛出InvalidOperationException
应该无法访问的代码


有任何线索吗?

聚合表达式需要特殊处理,请参阅。不幸的是,这个类不能用更多的行为和调用自定义重写器(请参阅)注入,因为聚合需要在调用之前处理

所有这些适用于NHibernate 5.1.2

在我看来,您使用的版本低于5,但您的版本可能与5相同


我认为您可能无法在不更改NHibernate代码的情况下处理您的自定义
TimeSpan
平均值。

我希望
linq to NHibernate
更灵活、更易于扩展。我开始了一个新的项目,在那里我有很多复杂的查询,我写的几乎每个查询最后都会重写到
HQL
,要么因为无法在
LINQ
中编写,要么可能的方法效率不高。谢谢你的回复!
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();