【问题标题】:nHibernate group by year and month with countnHibernate 按年和月分组,计数
【发布时间】:2014-02-13 12:19:36
【问题描述】:

我正在尝试获取一组文章/博客的所有年份和月份的列表。

var query = (from article in _session.Query<Article>()
            where article.Parent == articleList && article.PublishOn != null
            group article by new {article.PublishOn.Value.Year, article.PublishOn.Value.Month}
            into entryGroup
            orderby entryGroup.Key.Year descending, entryGroup.Key.Month descending
            select new ArchiveModel
            {
                Year = entryGroup.Key.Year,
                Month = entryGroup.Key.Month,
                Count = entryGroup.Count()
            });


return query.ToList();

上述编译但 nHibernate 抛出:

System.NotSupportedException {"NewExpression"}

有没有一种方法可以使用 nHibernate 中的任何其他查询方法来创建此查询?

我确实设法使用 SQL Projects 并使用 YEAR(date) 让它工作,但是这并没有在 SQLlite 中运行。如果可能,我需要它与数据库无关。

【问题讨论】:

  • 你使用什么版本的 NHibernate?

标签: c# nhibernate linq-to-nhibernate nhibernate-criteria


【解决方案1】:

如果有人偶然发现这个问题,我们设法解决了这个问题。问题是顺序。取出这个解决了错误:

var query = (from article in _session.Query<Article>()
                     where article.Parent == articleList && article.PublishOn != null
                     group article by new { article.PublishOn.Value.Year, article.PublishOn.Value.Month } into entryGroup
                     select new ArchiveModel
                     {
                         Year = entryGroup.Key.Year,
                         Month = entryGroup.Key.Month,
                         Count = entryGroup.Count()
                     });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 2011-07-21
    • 2021-10-16
    相关资源
    最近更新 更多