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();
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With