2014-02-13 231 views
1

我試圖列出一組文章/博客的所有年份和月份。nHibernate按年份和月份計算

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(日期)工作,但是這並沒有在SQLlite中運行。如果可能,我需要它是數據庫不可知的。

+0

你使用的是什麼版本的NHibernate? – hazzik

回答

4

設法解決這個問題,如果有人絆倒這個問題。問題在於順序。解決這個錯誤:

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