My LINQ queries can get super-complex, so in order to solve this I ended up writing a recursive parsing system. So my query visitor would call an expression parser to parse an expression, and then the expression would be a SubQueryExpression, which would trigger a call back into the QueryModel.
Now, for the group by operator – it is a ResultOperator – just like “Count()”, or “Sum()”. The difference is that it is a sequence ResultOperator. So I added extra code to deal with a type of result operator that was a sequence rather than just a single value.
I hope this helps. You are more than welcome to look at my code, but my application is so specific. Trying to translate LINQ to procedural C++ in this conext means the final result strongly influenced the architecture I used (also, when I started this I was “hacking” and didn’t know what I was doing, so probably made some important decision without realizing it).
The top level query processor. Note the code for parsing the result operators: https://github.com/gordonwatts/LINQtoROOT/blob/master/LINQToTTree/LINQToTTreeLib/QueryVisitors/QueryVisitor.cs#L69 (in particular the bit about a sequence result operator).
The group by result operator: https://github.com/gordonwatts/LINQtoROOT/blob/master/LINQToTTree/LINQToTTreeLib/ResultOperators/ROGroup.cs#L49
It may well be understanding my code is more effort than just forging ahead with some test cases in your environment. 😉