I am trying to get a list of all Years and Months for a set of articles/blogs.

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();

The above compiles but nHibernate throws:

System.NotSupportedException {"NewExpression"}

Is there a way I can create this query using any of the other query methods in nHibernate?

I did manage to get it working using SQL Projects and using YEAR(date) but then this didn't run in SQLlite. I need it to be database agnostic if possible.

有帮助吗?

解决方案

Managed to fix this if anyone stumbles across this question. The problem was the order by. Taking this out solved the error:

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()
                     });
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top