简体   繁体   中英

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM