Detecting whether a resultoperator is applied to the outer query

Skip to first unread message

Gunnar Liljas

Oct 26, 2017, 10:51:31 AM10/26/17
to re-motion Users
Consider a custom extension method, let's call it "Cacheable()", that can be applied to a query. It adds result operators (should they be sequence preserving).

Now I want to detect whether they've been added to the outer query (even it's been through such things Select, GroupBy, Join etc.), in which case I will keep them, otherwise they will be thrown away.

A lot of things adds SubQueryExpressions, so just detecting that is not enough.

Any suggestions?


Fabian Schmied

Oct 29, 2017, 2:43:50 PM10/29/17
Hi Gunnar,

How do you define "outer query"?

The QueryModel returned by re-linq is designed to have a similar structure as a C# syntactic sugar LINQ query (the "from ... where ... from ... join ... order by ... select ..." syntax, followed by other query operators modifying the query result). In general, re-linq will introduce subqueries only when it needs to so it can represent the actual query within this model.

As an example, consider an "OrderBy" placed after a "Take" - here, a subquery is required to represent the query as a QueryModel (because "Take" is at the "end" of the query, but "OrderBy" is in the middle). The following three queries are all equivalent (I think), so re-linq cannot differentiate them.

var query1 = inner.Cacheable().Take(10).OrderBy(x => x.Item);

var query2 = from x in inner.Cacheable().Take(10)
                  order by x.Item
                  select x;

var query3 = from x in inner.Cacheable().Take(10).OrderBy(x => x.Item)
                  select x;

re-linq's QueryModel will look like query2, wrapping "inner.Cacheable().Take(10)" into a SubQueryExpression. To me, it's now not clear if "Cacheable()" is part of the "outer query" or not. Regarding query1, it looks like it is. Regarding query2 and query3, it doesn't.

So, depending on your definition of "outer query", just checking whether the result operator is part of the "outermost" QueryModel (i.e., not embedded within a SubQueryExpression) could be enough. If it's not, you need to define what "outer query" means and then you can implement code checking for this condition.

Best regards,

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