我正在尝试对计数进行分组并按计数排序,但是我一直收到'Antlr.Runtime.NoViableAltException'的错误。
这里是我能够创建的最简单的错误情况。
var results = ArticleStatsRepository.GetAll().GroupBy(x => x.Article.ArticleId)
.OrderBy(x => x.Count());
ArticleStatsRepository.GetAll() 返回一个 ArticleStats 的 IQueryable。
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; }
}
最终我希望执行以下查询。
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 });
我正在使用Fluent NHibernate 1.2.0.712,它引用了NHibernate: 3.1.0.4000。非常感谢您的任何帮助!
问候
史蒂夫
更新:这就是我解决问题的方法。不完美,因为我不想开始使用具有其QueryOver的HQL,并且希望始终保持使用IQueryable。
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();
}
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