nhibernate/nhibernate-core — pull request #71 by hazzik (2012-02-01 08:42:58+00:00)
Title: NH 3044: Linq Self-joined query with subquery and order by - order by didn't apply the outer query

following query:

```csharp
var query = from log in _session.Query()
where log == _session.Query()
.OrderByDescending(l => l.DatePublished)
.First(l => l.ServiceName == log.ServiceName)
orderby log.ServiceName
select log;
```

The sql that gets generated looks like this:

```sql
select log0_.Id as Id1_,
log0_.DatePublished as DatePubl2_1_,
log0_.ServiceName as ServiceN3_1_,
log0_.Version as Version1_
from Log log0_
where log0_.Id = (select TOP (1) log1_.Id
from Log log1_
where log1_.ServiceName = log0_.ServiceName
order by log1_.DatePublished desc,
log0_.ServiceName asc);
```

Notice the ordering from "orderby log.ServiceName" ends up being shoved into the subquery, which is wrong. The resulting set will not be sorted.