开发者

Nhibernate 3 Linq throws Antlr.Runtime.NoViableAltException

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.Run开发者_运维百科time.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.


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜