10

I am trying to get a group by followed by an order by count to work but I keep getting a 'Antlr.Runtime.NoViableAltException' being thrown.

Here is the simplest error case I can create.

var results = ArticleStatsRepository.GetAll().GroupBy(x => x.Article.ArticleId)
               .OrderBy(x => x.Count()); 

ArticleStatsRepository.GetAll() returns an IQueryable of ArticleStats.

public class ArticleStats
{
    public virtual int ArticleStatsId { get; set; }
    public virtual Article Article { get; set; }
    public virtual User Viewer { get; set; }
    public virtual ArticleStatTypeEN ArticleStatType { get; set; }
    public virtual DateTime DateTime { get; set; }
}

Ultimately I would like the following query to execute.

return ArticleStatsRepository.GetAll()
                  .Where(x => x.DateTime > DateTime.Now.Add(-timeSpan))
                  .Where(x => x.ArticleStatType == ArticleStatTypeEN.View)
                  .GroupBy(x => x.Article.ArticleId)
                  .Select(x => new { ArticleId = x.Key, Count = x.Count() })
                  .OrderByDescending(x => x.Count)
                  .Join(ArticleRepository.GetAll(), artStats => artStats.ArticleId, articles => articles.ArticleId, (artStats, articles) => new MostPopularArticleResult { ArticleId = artStats.ArticleId, ArticleTitle = articles.Content.Title, Count = artStats.Count });

I am using Fluent NHibernate 1.2.0.712 which references NHibernate: 3.1.0.4000.

Any help would be greatly appreciated!

Regards

Steve

Update: This is how I got round the issue. Not perfect as I didn't want to start using HQL with its QueryOver and would of liked to stick to IQueryable throughout.

    public virtual IQueryable<MostPopularArticleResult> GetMostPopularArticleResults(TimeSpan timeSpan, IQueryable<Article> filteredArticles, List<ArticleStatTypeEN> types, int take)
    {
        var results = ArticleStatsRepository.GetAllQueryOver().Where(x => x.DateTime > DateTime.Now.Add(-timeSpan));

        results = results.Where(x => x.ArticleStatType.IsIn(types));

        var articleIdsWithCounts = results.Select(
                    Projections.Group<ArticleStats>(x => x.Article.ArticleId),
                    Projections.Count<ArticleStats>(x => x.Article.ArticleId))
                    .OrderBy(Projections.Count<ArticleStats>(x => x.Article.ArticleId))
                    .Desc
                    .Take(take)
                    .List<object[]>()
                    .Select(x => new { ArticleId = (int)x[0], Count = (int)x[1] });

        return articleIdsWithCounts.Join(filteredArticles, artStats => artStats.ArticleId, articles => articles.ArticleId, (artStats, articles) => new MostPopularArticleResult { ArticleId = artStats.ArticleId, ArticleTitle = articles.Content.Title, Count = artStats.Count })
            .AsQueryable();
    }
CountZero
  • 6,171
  • 3
  • 46
  • 59
  • Do I understand it correctly? You want to order by the number of elements in the group? – Daniel Hilgarth May 17 '11 at 11:32
  • Exactly, then I want to join those results to another table - I would then do a take so I only pull back x number of results. Ta – CountZero May 17 '11 at 12:49
  • Can you use QueryOver, or has to be LINQ? – psousa May 18 '11 at 09:15
  • Hi Psousa, I have decided to use the QueryOver for the time being. It's a pain as I wanted to stick to IQueryable as it makes things very easy to mock and I wanted to avoid having details about my Data Access Layer / ORM slipping into my business logic / service layer. Here is what I have. – CountZero May 18 '11 at 10:13
  • var results = ArticleStatsRepository.GetAllQueryOver().Where(x => x.DateTime > DateTime.Now.Add(-timeSpan)); results = results.Where(x => x.ArticleStatType.IsIn(types)); var articleIdsWithCounts = results.Select( Projections.Group(x => x.Article.ArticleId), Projections.Count(x => x.Article.ArticleId)) .OrderBy(Projections.Count(x => x.Article.ArticleId)) .Desc ... – CountZero May 18 '11 at 10:14
  • 3
    This is a known bug afaik - check in https://nhibernate.jira.com/secure/Dashboard.jspa (group by and where doesn't work so this might be related) – Chris S Aug 18 '11 at 18:02

1 Answers1

1

As @mynkow and @Chris S said that was a NH 3.1 issue. You could update library version or look at thoose question about simila problem:

http://sourceforge.net/p/nhibernate/mailman/nhibernate-issues/

Nhibernate 3 Linq throws Antlr.Runtime.NoViableAltException

NHibernate Query, using OfType and Subqueries

NHibernate 3.1 migration problem with Linq

Community
  • 1
  • 1
zeppaman
  • 844
  • 8
  • 23