nhibernate/nhibernate-core — pull request #95 by hazzik (2012-04-24 16:55:05+00:00)
Title: NH-3125: Linq - Invalid SQL when querying via LINQ with grouping (regression from 3.2)

The following LINQ query worked nicely in NHibernate 3.2 (simplified from real scenario)

```csharp
var result = from result in Session.Query()
let order = result.Task.Sample.Order
group result by new { TaskId = result.Task.Id, orderId = order.Id }
into temp
select new Projection(temp.Key.TaskId, temp.Count(), temp.Key.orderId);

result.ToList();
```

It generates the following SQL:

```SQL
select orderr0_.TaskId as col_0_0_,
order3_.Id as col_1_0_,
cast(count(*) as INT) as col_2_0_,
orderr0_.TaskId as col_3_0_,
order3_.Id as col_4_0_
from [Task] orderr0_
left outer join [Task] Ordert1_
on orderr0_.TaskId = Ordert1_.Id
left outer join [Sample] Orders2_
on Ordert1_.SampleId = Orders2_.Id
left outer join [Order] order3_
on Orders2_.OrderId = order3_.Id
left outer join [Task] Ordert4_
on orderr0_.TaskId = Ordert4_.Id
left outer join [Sample] Orders5_
on Ordert4_.SampleId = Orders5_.Id
group by orderr0_.TaskId,
Orders5_.OrderId
```

Problem there that `SelectJoinDetector` has a minor bug which in some cases makes `_identifierMemberExpressionDepth` negative and therefore detector is trying to add join for properies on incorrect level of indirection.

Also this pull request brings an improvement to `GroupBySelectClauseRewriter`. Now it is trying to simplify transparent properies of Key property of grouping representing as anonymous objects in select clause.

JIRA: https://nhibernate.jira.com/browse/NH-3125