How to handle grouped by sentences

Skip to first unread message

Jordi Cabré

Apr 13, 2016, 3:58:34 AM4/13/16
to re-motion Users
I'm interested in handle grouped sentences like:

var fs = this.backend.DigitalInputs.GroupBy(di => di.Channel).Select(g => new { Channel = g.Key, Count = g.Count() });

As far I've been able to figure out, I'm guessing "group" information in embedded into main QueryModel creating another nested QueryModel into mainQueryModel's MainFromClause:

QueryModel ->
* MainFromClause: {from IGrouping`2 g in {value(Backend.Implementations.Lest.Linq.LivingQueryable`1[Backend.Domain.DigitalInput]) => GroupBy([di].Channel, [di])}}
* FromExpression: {{value(Backend.Implementations.Lest.Linq.LivingQueryable`1[Backend.Domain.DigitalInput]) => GroupBy([di].Channel, [di])}}
      * QueryModel:
        * MainFromClause: {from DigitalInput di in value(Backend.Implementations.Lest.Linq.LivingQueryable`
* ResultOperators: (1) -> {GroupBy([di].Channel, [di])}

My question is about which would be the best or common strategy for going through this kind of nested query models?

My first approuch was around handling GroupResultOperator, however, I realized it's never reached.

Currently, I've a Transformer and a Translafor QueryModel visitors.

In my Transformer's VisitMainFromClause is a dummy method:

public override void VisitMainFromClause(MainFromClause fromClause, QueryModel queryModel)
    // do nothing
    base.VisitMainFromClause(fromClause, queryModel);

And my Translator's VisitMainFromClause is a bit less dummy method.
Basiclly, checks out whether fromClause.ItemType is aknowledged.
However, with a groupby information in it, this fromClause.ItemType is like a Grouping`1...:

public override void VisitMainFromClause(MainFromClause fromClause, QueryModel queryModel)
.Expressions.Catalog.LQLEntityExpression entity = fromClause.ItemType.GetDomainEntity();
this.lqlQueryModel.Search = entity;


Gordon Watts

Apr 16, 2016, 7:33:11 PM4/16/16
to Jordi Cabré, re-motion Users


  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: (in particular the bit about a sequence result operator).


  The group by result operator:


It may well be understanding my code is more effort than just forging ahead with some test cases in your environment. 😉



You received this message because you are subscribed to the Google Groups "re-motion Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
For more options, visit
Reply all
Reply to author
0 new messages