Note that there are some explanatory texts on larger screens.

plurals
  1. PONHIbernate (3.1) - Linq group by then order by count issue
    primarykey
    data
    text
    <p>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.</p> <p>Here is the simplest error case I can create.</p> <pre><code>var results = ArticleStatsRepository.GetAll().GroupBy(x =&gt; x.Article.ArticleId) .OrderBy(x =&gt; x.Count()); </code></pre> <p>ArticleStatsRepository.GetAll() returns an IQueryable of ArticleStats. </p> <pre><code>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; } } </code></pre> <p>Ultimately I would like the following query to execute. </p> <pre><code>return ArticleStatsRepository.GetAll() .Where(x =&gt; x.DateTime &gt; DateTime.Now.Add(-timeSpan)) .Where(x =&gt; x.ArticleStatType == ArticleStatTypeEN.View) .GroupBy(x =&gt; x.Article.ArticleId) .Select(x =&gt; new { ArticleId = x.Key, Count = x.Count() }) .OrderByDescending(x =&gt; x.Count) .Join(ArticleRepository.GetAll(), artStats =&gt; artStats.ArticleId, articles =&gt; articles.ArticleId, (artStats, articles) =&gt; new MostPopularArticleResult { ArticleId = artStats.ArticleId, ArticleTitle = articles.Content.Title, Count = artStats.Count }); </code></pre> <p>I am using Fluent NHibernate 1.2.0.712 which references NHibernate: 3.1.0.4000.</p> <p>Any help would be greatly appreciated!</p> <p>Regards</p> <p>Steve</p> <p>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. </p> <pre><code> public virtual IQueryable&lt;MostPopularArticleResult&gt; GetMostPopularArticleResults(TimeSpan timeSpan, IQueryable&lt;Article&gt; filteredArticles, List&lt;ArticleStatTypeEN&gt; types, int take) { var results = ArticleStatsRepository.GetAllQueryOver().Where(x =&gt; x.DateTime &gt; DateTime.Now.Add(-timeSpan)); results = results.Where(x =&gt; x.ArticleStatType.IsIn(types)); var articleIdsWithCounts = results.Select( Projections.Group&lt;ArticleStats&gt;(x =&gt; x.Article.ArticleId), Projections.Count&lt;ArticleStats&gt;(x =&gt; x.Article.ArticleId)) .OrderBy(Projections.Count&lt;ArticleStats&gt;(x =&gt; x.Article.ArticleId)) .Desc .Take(take) .List&lt;object[]&gt;() .Select(x =&gt; new { ArticleId = (int)x[0], Count = (int)x[1] }); return articleIdsWithCounts.Join(filteredArticles, artStats =&gt; artStats.ArticleId, articles =&gt; articles.ArticleId, (artStats, articles) =&gt; new MostPopularArticleResult { ArticleId = artStats.ArticleId, ArticleTitle = articles.Content.Title, Count = artStats.Count }) .AsQueryable(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload