NHIbernate (3.1) - Linq按组排序后再按计数排序的问题

10

我正在尝试对计数进行分组并按计数排序,但是我一直收到'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();
    }

我理解的对吗?您想按组中元素的数量排序? - Daniel Hilgarth
没错,然后我想将这些结果与另一个表连接起来 - 然后我会进行取样,以便只获取x个结果。谢谢。 - CountZero
你能使用QueryOver吗,还是必须使用LINQ? - psousa
嗨,Psousa,我已经决定暂时使用QueryOver。这很痛苦,因为我想坚持使用IQueryable,因为它使模拟变得非常容易,并且我想避免我的数据访问层/ORM的细节泄漏到我的业务逻辑/服务层中。这是我的代码。 - CountZero
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
3
据我所知,这是一个已知的错误 - 请在 https://nhibernate.jira.com/secure/Dashboard.jspa 中检查(按组排序和where操作不起作用,因此可能与此有关)。 - Chris S
1个回答

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接