score:3

Accepted answer

It is possible to change expression trees at runtime (by building a new one out of the existing one), since O/RM tools such as LINQ to SQL and Entity Framework do this constantly, but it's not really easy. It has become easier with the introduction of the ExpressionVisitor class of .NET 4.0, but still don't expect it to be simple.

Here is an article that shows an example of this.

score:0

Would it help to convert the query to an Enumerable and call Count() on this like this:

var num = query.AsEnumerable().Count();

This would execute the query and afterwards makes a simple Count() on the result instead of letting the Count() flow into the ExpressionTree.


Related Articles