[英]Setting max equals-to in projections with nHibernate
我有一個要轉換為nHibernate的t-sql查詢。
我已經很近了,但是我很難在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 :
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
有什么建議么?
我想到了:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.