简体   繁体   English

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

[英]Setting max equals-to in projections with nHibernate

I have a t-sql query I'm converting to nHibernate. 我有一个要转换为nHibernate的t-sql查询。

I've gotten close, but I'm having difficultly around the max function 我已经很近了,但是我很难在max函数附近

My attempted: 我的尝试:

C# 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))

Output: 输出:

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

This is the SQL I'm trying to achieve : 这是我正在尝试实现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

Any suggestions? 有什么建议么?

I figured it out: 我想到了:

C# 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);

Sort Method: 排序方式:

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

Output Sql: 输出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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM