2011-08-01 19 views
1

我有一個t-sql查詢轉換爲nHibernate。在nHibernate中設置最大等於 - 投影

我已經得到接近,但我在各地的max函數難

我嘗試:

C#

var itemsQuery = queryOver.Clone() 
    .OrderBy(a =>a.liveStartTime).Asc 
    .Select(Projections.GroupProperty(Projections.Property<ChannelFind>(a => a.channelID)), Projections.Max<ChannelFind>(a => a.liveStartTime)) 

輸出:

SELECT TOP (20 /* @p0 */) this_0_.channelID   as y0_, 
              max(this_0_.liveStartTime) as y1_ 
         FROM  vTGv0_channel_Find this_0_ 
         GROUP BY this_0_.channelID 
         ORDER BY this_0_.liveStartTime asc 

這是SQL 0的我試圖實現

SELECT TOP (20 /* @p0 */) this_0_.channelID as y0_, liveStartTime = max(this_0_.liveStartTime) 
          FROM  vTGv0_channel_Find this_0_ 
          GROUP BY this_0_.channelID 
          ORDER BY liveStartTime asc 

有什麼建議?

回答

0

我想通了:

C#

 var itemsQuery = queryOver.Clone() 
      .Where(a => a.channelID != null) 
      .OrderBy(Projections.Max<ChannelFind>(a => a.liveStartTime)); 

     var sortedQuery = Sort(itemsQuery, sort) 
      .Select(Projections.GroupProperty(Projections.Property<ChannelFind>(a => a.channelID))) 
      .Skip(index ?? 0) 
      .Take(itemsPerPage ?? 20); 

排序方法:

protected static IQueryOver<T, T> Sort<T, Q>(QueryOverOrderBuilderBase<Q, T, T> order, string sort) where Q : IQueryOver<T, T> 
    { 
     IQueryOver<T, T> query = (string.Equals("asc", sort, StringComparison.CurrentCultureIgnoreCase) ? order.Asc : order.Desc); 
     return query; 
    } 

輸出SQL:

SELECT TOP (20 /* @p0 */) this_0_.channelID as y0_ 
         FROM  vTGv0_channel_Find this_0_ 
         WHERE not (this_0_.channelID is null) 
         GROUP BY this_0_.channelID 
         ORDER BY max(this_0_.liveStartTime) asc