开发者

Group by and order by

开发者 https://www.devze.com 2022-12-24 04:49 出处:网络
using LINQ to NHibernate does anybody know how to use group by and order by in the same expression.开发者_运维技巧I am having to execute the group by into a list and then order this, seem that I am mi

using LINQ to NHibernate does anybody know how to use group by and order by in the same expression. 开发者_运维技巧I am having to execute the group by into a list and then order this, seem that I am missing soemthing here ???

Example:-

Private function LoadStats(...) ...
  Dim StatRepos As DataAccess.StatsExtraction_vwRepository = New DataAccess.StatsExtraction_vwRepository

  return (From x In StatRepos.GetAnswers(Question, Questionnaire) _
              Group x By xData = x.Data Into Count() _
              Select New ChartData 
                   With {.TheData = xData, 
                         .TheValue = xData.Count}
         ).ToList.OrderBy(Function(x) x.TheData)

End Sub


I'm not a Visual Basic expert, but you should be able to use alias in the Select clause to create a temporary variable, so that you can refer to it in a later clauses such as Order By. Unless there is some limitation in NHibernate, the following should work:

return From x In StatRepos.GetAnswers(Question, Questionnaire) _ 
       Group x By xData = x.Data Into Count() _ 
       Select res = New ChartData With _
         { .TheData = xData,  _
           .TheValue = xData.Count } _
       Order By res.TheData _
       Select res

I guess that in some situations, this may be more readable than moving the Order By clause before the Select clause.

0

精彩评论

暂无评论...
验证码 换一张
取 消