Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Linq query: .Where chain vs &&

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?

like image 745
bunglestink Avatar asked Feb 17 '11 13:02

bunglestink


2 Answers

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

like image 78
Aducci Avatar answered Nov 14 '22 01:11

Aducci


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.

like image 31
Hennish Avatar answered Nov 14 '22 00:11

Hennish