We're updating the issue view to help you get more done. 

Left Outer Join with Aggregating Group By and Conditional Key Failure


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

1 var query = baseQuery.Select(x => new object[] { x }).GroupBy(array => new object[] { ((Entity)array[0]).Prop == value ? "A" : "B" }).Select(g => new { Key = g.Key, Count = g.Count());

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

1 new object[] { ((Entity)(new object[] { x }[0])).Prop == value ? "A" : "B" }

And this gets translated to

1 new object[] { ((Entity)(<nominated>[0])).Prop == value ? "A" : "B" }

When what needs to happen is

1 new object[] { <nominated> }







Duncan Munro


Fix versions

Affects versions