I'm having an issue currently where I have to apply sorting twice in a query.
Since I want paginated results, I'm using a DetachedCriteria approach and using it as a Subquery for the main Criteria.
Issue is, that if I apply the sorting only on the DetachedCriteria, I don't get back the results sorted.
This is because the sql "IN" clause does not guarantee that the results will come back sorted.
So I need to apply the same sorting again on the main Criteria.
var detachedCriteria = DetachedCriteria.For<User>()
.SetProjection(Projections.Id())
.setMaxResults(20).setFirstResult(0)
.AddOrder(Order.Asc("Name"));
session.CreateCriteria<User>
.Add(Subqueries.PropertyIn("Id", detachedCriteria))
.AddOrder(Order.Asc("Name"))
.List();
This would work, if could instead apply the DetachedCriteria as a JOIN clause, but as far as I know this is not supported by NHibernate.
Does anybody know of a different approach?