使用nHibernate在投影中设置max equals-to

时间:2011-08-01 22:44:03

标签: c# nhibernate

我有一个t-sql查询,我正在转换为nHibernate。

我已经接近了,但我很难绕过最大功能

我的尝试:

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

有什么建议吗?

1 个答案:

答案 0 :(得分: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