Nhibernate 3 Linq 抛出 Antlr.Runtime.NoViableAltException

发布于 2024-10-31 20:15:11 字数 1852 浏览 0 评论 0原文

使用 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, ))), ), )]

这是一个已知问题吗?或者我是否必须重写查询,任何建议都非常受欢迎。

Using the NHibernate 3 linq provider I would like to select the max count of an items children.

Using the following linq query I get an Antlr.Runtime.NoViableAltException followed by an Antlr.Runtime.MismatchedTreeNodeException

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

The repository function FindAll() returns session.Query.

Exception details:

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, ))), ), )]

Is this a known problem? Or do I have to rewrite the query, any suggestions are more then welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

去了角落 2024-11-07 20:15:11

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

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

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

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

Apparently the current NHibernate Linq provider cannot combine Max() and an inner Select in the Select clause. You may need to pull the Max out of the Query and apply it afterwards, e.g.

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

A simpler version without a sub select works:

int maxCount = session.Query<Device>()
    .Select(d => d.Name.Length)
    .Max();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文