Order by alias

29 views
Skip to first unread message

Ted P

unread,
Jan 2, 2013, 3:45:18 PM1/2/13
to nhu...@googlegroups.com
If I have a query where I'm creating a sql-projected select, and alias it, what's the proper way to add ordering of this aliased projection? 
The QueryOver api only has an "OrderByAlias" that takes a function from which it extracts a member, hence I can't use it.
Right now I'm resorting to the following which works but looks kind of hacky to me (the projection2 part)

var projection = Projections.SqlProjection("COUNT({alias}.ReportId) OVER(PARTITION BY {alias}.ReportId) AS messageCount", new[] { "messageCount" }, new IType[] { NHibernate.NHibernateUtil.Int32});
var projection2 = Projections.SqlProjection("messageCount as messageCount", new[] { "messageCount" }, new IType[] { NHibernate.NHibernateUtil.Int32});

var resuls = Session.QueryOver<Report>()
                    .Fetch(r => r.Messages).Eager
                    .SelectList(list => list
                        .Select(Projections.Distinct(Projections.Id()))
                        .Select(projection)
                    ).OrderBy(projection2).Desc.Take(100).List<object>();

Never mind the actual query, that could be done in other ways, I'm only interested in how the "OrderBy" ought to be.

I also tried falling back to ICriteria, replacing the OrderBy like so:
...UnderlyingCriteria.AddOrder(Order.Desc("messageCount")).SetMaxResults(100).List<object>();

But this fails on the account that "messageCount" is expected to be a property of the Report entity.

And a follow up question. NHibernate still only supports the "{alias}" to refer to the root entity in a projection? No way to reference any joined aliases?

Cheers!
Reply all
Reply to author
Forward
0 new messages