【问题标题】:Nhibernate 3 Linq throws Antlr.Runtime.NoViableAltExceptionNhibernate 3 Linq 抛出 Antlr.Runtime.NoViableAltException
【发布时间】:2011-04-13 10:52:22
【问题描述】:

使用 NHibernate 3 linq 提供程序,我想选择子项的最大计数。

使用以下 linq 查询,我得到一个 Antlr.Runtime.NoViableAltException,后跟一个 Antlr.Runtime.MismatchedTreeNodeException

int maxCount = _repository.FindAll<Device>().Max(d=>d.DeviceSensors.Count());

存储库函数 FindAll() 返回 session.Query。

异常详情:

Antlr.Runtime.NoViableAltException occurred
  Message="Exception of type 'Antlr.Runtime.NoViableAltException' was thrown."
  Source="NHibernate"
  Char=0
  CharPositionInLine=-1
  Index=21
  Line=0
  UnexpectedType=84
  StackTrace:
       at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.aggregateExpr() in d:\CSharp\NH\nhibernate\src\NHibernate\Hql\Ast\ANTLR\Generated\HqlSqlWalker.cs:line 3203
  InnerException: 

.

Antlr.Runtime.MismatchedTreeNodeException occurred
  Message="Exception of type 'Antlr.Runtime.MismatchedTreeNodeException' was thrown."
  Source="Antlr3.Runtime"
  Char=0
  CharPositionInLine=-1
  Index=21
  Line=0
  UnexpectedType=84
  StackTrace:
       at Antlr.Runtime.Tree.TreeParser.RecoverFromMismatchedToken(IIntStream input, Int32 ttype, BitSet follow)
       at Antlr.Runtime.BaseRecognizer.Match(IIntStream input, Int32 ttype, BitSet follow)
       at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.functionCall() in d:\CSharp\NH\nhibernate\src\NHibernate\Hql\Ast\ANTLR\Generated\HqlSqlWalker.cs:line 7906
  InnerException:

 NHibernate.Hql.Ast.ANTLR.QuerySyntaxException: Exception of type 'Antlr.Runtime.NoViableAltException' was thrown. [.First(.Select(.OrderByDescending(NHibernate.Linq.NhQueryable`1[RGB.TTT.Domain.Device], Quote((x, ) => (x.DeviceSensors.Count)), ), Quote((x, ) => (new <>f__AnonymousType2`1(x.DeviceSensors.Count, ))), ), )]

这是一个已知问题吗?还是我必须重写查询,欢迎提出任何建议。

【问题讨论】:

    标签: c# linq nhibernate


    【解决方案1】:

    显然,当前的 NHibernate Linq 提供程序无法在 Select 子句中结合 Max() 和内部 Select。您可能需要将 Max 从 Query 中拉出并在之后应用它,例如

    int maxCount = session.Query<Device>()
        .Select(d => d.DeviceSensors.Count)
        .ToList()
        .Max();
    

    没有子选择的更简单的版本可以工作:

    int maxCount = session.Query<Device>()
        .Select(d => d.Name.Length)
        .Max();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多