Count
and GroupBy
are methods. So you first need to get the corresponding MethodInfo
s using reflection (take care to get the methods from the Queryable
class, not Enumerable
, and the correct overload!).
Then create a method call expression using Expression.Call(null, methodInfo, queryable)
. (The first parameter should be null
since both methods are static.) Here, queryable
is the IQueryable
you already have built up to this point on which you want to call Count
or GroupBy
, respectively. For GroupBy
, you also need to add an extra parameter, which is the lambda expression for projecting the City
property - I assume you know how to build that.
Update
But if I understand your motivation correctly, you are actually trying to dynamically build an IQueryable
with where-clauses, selects, groupby and/or counts, based on the base IQueryable
you get from the library, is that correct?
In that case you shouldn't express Count
and GroupBy
as expressions. Rather, you should dynamically call the real methods Where
, Select
, Count
and GroupBy
on your base IQueryable
(to get another IQueryable
), but dynamically build lambda expressions for the parameters of these methods. In the end, you will have an IQueryable
which the Cosmos DB LINQ provider can evaluate.