25

This question pertains to query optimization using Linq with the Entity Framework.

Is there any difference between chaining .Where clauses and using && in a single .Where clause of a linq query with the entity framwork?

E.g.: suppose I have the following code:

var result = context.SomeEntity.Where(exp1).Where(exp2);

or

var result = context.SomeEntity.Where(exp1 && exp2);

When evaluating these statements, which yield the same result, does the linq and the entity framework evaluate them in the same way? i.e., will both have the same execution plan, and therefore be equally efficient?

Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
bunglestink
  • 768
  • 8
  • 16

2 Answers2

23

Yes both will have the same execution plan. I have added a sql trace and both create the same SQL statements

Aducci
  • 26,101
  • 8
  • 63
  • 67
3

I always go with the && because it makes the code easier to read. By using multiple Where this will create a lot of extra noise and take focus off of the logic.

Gonzalo.-
  • 12,512
  • 5
  • 50
  • 82
Hennish
  • 685
  • 6
  • 10
  • 4
    It's easier to read if the && statements are short and few, but if you're chaining together multiple longer statements, the .Where chaining makes more sense and is more readable. – pkr Jun 25 '13 at 14:41