Skip to content
Advertisement

Performance for using 2 where clauses in LINQ

In LINQ-to-Entities you can query entities by doing:

I know that behind the scenes it will be translated to SQL to something similar to:

However is there a difference (with respect to performance) if I write:

Will it be translated to the same SQL query? From my understanding .Where() will return IEnumerable<T> so the second .Where() will filter the entities in-memory instead of translating the IQueryable<T> to SQL, is that correct?

Advertisement

Answer

The first .Where() clause will still return an IQueryable<T>. As long as you are operating on an IQueryable<T> it will continue building up the SQL query and execute it when the collection needs to be brought into memory (eg: as @anaximander stated when used in a foreach loop or ToList() operation.

Therefore:

Still translates into:

Whilst the below 2 statements will translate into the same query:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement