简体   繁体   中英

Setting max equals-to in projections with nHibernate

I have a t-sql query I'm converting to nHibernate.

I've gotten close, but I'm having difficultly around the max function

My attempted:

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 :

   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#

        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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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