Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHIbernate (3.1) - Linq group by then order by count issue

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();
    }
like image 717
CountZero Avatar asked May 17 '11 10:05

CountZero


1 Answers

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

like image 54
zeppaman Avatar answered Oct 10 '22 02:10

zeppaman