FatAlbert
FatAlbert

Reputation: 4970

Is there a difference in performance how a linq expression is constructed?

Is there a difference in performance depending on where the where clause is located in a linq expression?

Take a look at the code below:

dbContext.AnEntity.Include("AnotherEntity").Where(e => e.ID == id).ToList();

dbContext.AnEntity.Where(e => e.ID == id).Include("AnotherEntity").ToList();

Is the execution plan of these two expression the same or do the differ?

Upvotes: 4

Views: 103

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062510

In theory, no. In reality, you will need to test on a case-by-case basis, and on a provider-by-provider bases. For a related (but slightly different) example, for a good long while in L2S, .Where(predicate).FirstOrDefault() and .FirstOrDefault(predicate) (which have the same semantic) did not have the same behaviour (in particular relating to identity-map shortcuts).

I expect it will be the same, especially since .Include (expands the result set) is in a bit of a separate categry than .Where (a predicate) but: the only way to verify would be to test it and compare the generated SQL and performance.

Upvotes: 1

Related Questions