Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order execution of chain linq query

Is there a difference in this code?

var query = DbContext.Customers
                .Where(<condition>)
                .Include("Address");

And

var query = DbContext.Customers
                .Include("Address")
                .Where(<condition>);

It's deffered query, and I don't know, is it equivalent? Or in the second case where is executed after Include?

Thanks.

like image 819
Daniil Grankin Avatar asked Jul 25 '13 08:07

Daniil Grankin


1 Answers

For EF order prior to the select does not matter. The LINQ query is converted to a SQL query and run and the SQL query optimizer does not care about the order of the original query.

As Patryk pointed out order can matter specifically with Include when the following statements modify the structure of the query, but a where clause doesn't do that.

In other LINQ queries, LINQ-to-Objects, order can matter greatly as the query is not re-optimized the way SQL is and is simply processed from top to bottom, and some LINQ methods require previous methods to run to completion and process results before proceeding to even the first element (OrderBy for example).

like image 75
Samuel Neff Avatar answered Oct 07 '22 23:10

Samuel Neff