0

I want to count rows for a certain query for which I build the WHERE-clause dynamically.

What excited me was the difference I saw when building this in two different ways.

Try 1

int resultsCount = context.MyView.Where(x => x.Id > 100000).Count();

in the profiler I see, that the following query is being executed:

SELECT 
[GroupBy1].[A1] AS [C1]
FROM ( SELECT 
    COUNT(1) AS [A1]
    FROM (SELECT 
      [MyView].[Id] AS [Id], 
      [MyView].[EventTypeId] AS [EventTypeId], 
      [MyView].[EventSourceId] AS [EventSourceId], 
      [MyView].[TraceLevelId] AS [TraceLevelId], 
      [MyView].[TimeCreated] AS [TimeCreated], 
      [MyView].[Data1MaxStr] AS [Data1MaxStr]
      FROM [dbo].[MyView] AS [MyView]) AS [Extent1]
    WHERE [Extent1].[Id] > 100000
)  AS [GroupBy1]

Try 2

But I have to check for multiple conditions and not all of them need to be met for every execution. So I tried building the WHERE-clause for my COUNT dynamically (referring to this post):

var parameter = Expression.Parameter(typeof(MyView), "x");
Expression<Func<MyView, bool>> check1 = x => x.Id > 100000;
Expression expression = Expression.Constant(true);
expression = Expression.AndAlso(expression, Expression.Invoke(check1, parameter));
var lambda = Expression.Lambda<Func<MyView, bool>>(expression, parameter);

int resultsCount = context.MyView.Where(lambda.Compile()).Count();

I would expect to see the same or at least a similar query being executed, but instead I see the following in the profiler:

SELECT 
[Extent1].[Id] AS [Id], 
[Extent1].[EventTypeId] AS [EventTypeId], 
[Extent1].[EventSourceId] AS [EventSourceId], 
[Extent1].[TraceLevelId] AS [TraceLevelId], 
[Extent1].[TimeCreated] AS [TimeCreated], 
[Extent1].[Data1MaxStr] AS [Data1MaxStr]
FROM (SELECT 
      [MyView].[Id] AS [Id], 
      [MyView].[EventTypeId] AS [EventTypeId], 
      [MyView].[EventSourceId] AS [EventSourceId], 
      [MyView].[TraceLevelId] AS [TraceLevelId], 
      [MyView].[TimeCreated] AS [TimeCreated], 
      [MyView].[Data1MaxStr] AS [Data1MaxStr]
      FROM [dbo].[MyView] AS [MyView]) AS [Extent1]

The value of resultsCount however is the same, but why are those queries so different and where does LinQ get the COUNT for the second query, because I do not see any COUNT being selected?

Can someone tell me how I can force a query similar to the first but still build my WHERE-clause dynamically?

Any hints would be much appreciated.

7
  • most probably on Try2 you should include counting into the expression bulding before it is compiled Sep 30, 2013 at 13:52
  • how would I do that, I only know how to append COUNT to a query for example instead of a SELECT Sep 30, 2013 at 13:54
  • line expression = Expression.AndAlso(expression, Expression.Invoke(check1, parameter)); was missing updated it Sep 30, 2013 at 14:01
  • Expression<Func<IEnumerable<MyView>, long>> countExpression = x => x.Count(); and then use as in the example about. Probably smth like this? Sep 30, 2013 at 14:07
  • Sorry, I still don't understand, where should that countExpression go? I cannot include it into the WHERE with AndAlso, can I? It probably would have to substitute the Count()-call I am currently using, right? But how? Sep 30, 2013 at 14:14

1 Answer 1

1

You're calling Compile on your expression.

This transforms it from an Expression<Func<MyView, bool>> into a Func<MyView, bool>. Since it's just a delegate, and not an Expression, it no longer matches the signature for the IQueryable overload of Where, it only matches the IEnumerable overload.

This means that the query isn't translated into SQL; you are querying the data with no filtering at all, and then doing the filtering in LINQ to objects on the client side.

It's important that you not compile the lambda if you want the query to be translated into SQL by the query provider.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.