如何解决Nhibernate 3 Linq抛出Antlr.Runtime.NoViableAltException
| 使用NHibernate 3 linq提供程序,我想选择子项的最大数量。 使用以下linq查询,我得到一个Antlr.Runtime.NoViableAltException,然后是一个Antlr.Runtime.MismatchedTreeNodeExceptionint 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,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)),),) => (new <>f__AnonymousType2`1(x.DeviceSensors.Count,))),)]
这是一个已知问题吗?还是我必须重写查询,欢迎提出任何建议。
解决方法
显然,当前的NHibernate Linq提供程序无法在Select子句中组合Max()和内部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();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。