Once again, I have a weirdly specific use case that tries to combine joining, grouping, and conditionals. Actually, it's the same issue that I thought I had solved almost half a year ago with and NH-3801.
The previous fixes are working well, but I found a corner case the breaks down.
The code I'm running is generating a LINQ query programmatically that first tries to resolve a number of outer joins. To do this, I wrap my initial entity in an object array. Then as I'm building the group by clauses I extract the required entity (or entities) from the object array and build by group by key as an object array.
So (at minimum) you get something like this
Only more complicated....
Anyway, the whole thing fails because the group key nomination happens before the array flattening.
The group by expression essentially looks like
And this gets translated to
When what needs to happen is