NHibernate产生System.ArgumentException

时间:2016-02-17 17:54:59

标签: c# .net linq nhibernate fluent-nhibernate

我正在尝试将对DB的两个请求优化为如下所示:

    var subQuery = Session
        .Query<IProfile>()
        .Where(profile => accountIds.Contains(profile.AccountId))
        .GroupBy(profile => profile.AccountId)
        .Select(kv => kv.Max(profile => profile.Id));
        //.ToList();

    var outQuery = Session
        .Query<IProfile>()
        .Where(profile => subQuery.Contains(profile.Id))
        .Fetch(profile => profile.User)
        .Fetch(profile => profile.Email);

    var result = outQuery.ToList();

但是这产生了一个System.ArgumentException

但如果是这样的两个单独的DB请求

    var subQuery = Session
        .Query<IProfile>()
        .Where(profile => accountIds.Contains(profile.AccountId))
        .GroupBy(profile => profile.AccountId)
        .Select(kv => kv.Max(profile => profile.Id))
        .ToList();

一切正常。

有关例外的更多信息:

  

'System.Nullable1 [System.Int64]'不能用作a的数据类型   具有ItemExpression类型的序列   'System.Nullable1 [System.Int64]'。参数名称:dataType

     

在   Remotion.Linq.Clauses.StreamedData.StreamedSequenceInfo.AdjustDataType(类型   dataType)在Remotion.Linq.QueryModel.GetOutputDataInfo()at   Remotion.Linq.QueryModel.GetResultType()at   NHibernate.Linq.NestedSelects.NestedSelectRewriter.ProcessSubquery(ISessionFactory   sessionFactory,ICollection1 elementExpression,QueryModel queryModel,   表达式组,QueryModel(subQueryModel))   NHibernate.Linq.NestedSelects.NestedSelectRewriter.ProcessExpression(QueryModel   queryModel,ISessionFactory sessionFactory,Expression表达式,   List1 elementExpression,ParameterExpression group)at   NHibernate.Linq.NestedSelects.NestedSelectRewriter.ReWrite(QueryModel   queryModel,ISessionFactory sessionFactory)at   NHibernate.Linq.Visitors.QueryModelVisitor.GenerateHqlQuery(QueryModel   queryModel,VisitorParameters参数,布尔根)   NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitSubQueryExpression(SubQueryExpression   表达)at   NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitExpression(表达式   表达)at   NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.Visit(表达式   表达式,VisitorParameters参数)at   NHibernate.Linq.Visitors.QueryModelVisitor.VisitWhereClause(WhereClause   whereClause,QueryModel queryModel,Int32 index)at   Remotion.Linq.Clauses.WhereClause.Accept(IQueryModelVisitor visitor,   QueryModel queryModel,Int32 index)at   Remotion.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection1   bodyClauses,QueryModel queryModel)at   Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel   queryModel)at NHibernate.Linq.Visitors.QueryModelVisitor.Visit()
  在   NHibernate.Linq.Visitors.QueryModelVisitor.GenerateHqlQuery(QueryModel   queryModel,VisitorParameters参数,布尔根)   NHibernate.Linq.NhLinqExpression.Translate(ISessionFactoryImplementor   sessionFactory,布尔过滤器)at   NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IQueryExpression   queryExpression,String collectionRole,Boolean shallow,IDictionary2   过滤器,ISessionFactoryImplementor工厂)at   NHibernate.Engine.Query.QueryExpressionPlan.CreateTranslators(IQueryExpression   queryExpression,String collectionRole,Boolean shallow,IDictionary2   enabledFilters,ISessionFactoryImplementor factory)at   NHibernate.Engine.Query.QueryExpressionPlan..ctor(IQueryExpression   queryExpression,布尔浅,IDictionary2 enabledFilters,   ISessionFactoryImplementor工厂)在   NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression   queryExpression,Boolean shallow,IDictionary2 enabledFilters)at   NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression   queryExpression,布尔浅)at   NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression   queryExpression)at   NHibernate.Linq.DefaultQueryProvider.PrepareQuery(表达式   表达,IQuery&amp;查询,NhLinqExpression&amp; nhQuery)at   NHibernate.Linq.DefaultQueryProvider.Execute(表达式)   在NHibernate.Linq.DefaultQueryProvider.Execute [TResult](表达式   表达式)在Remotion.Linq.QueryableBase1.GetEnumerator()at   System.Collections.Generic.List1..ctor(IEnumerable1集合)at   System.Linq.Enumerable.ToList [TSource](IEnumerable1 source)

1 个答案:

答案 0 :(得分:0)

您是否可以尝试重写这两个查询:

lambda