Order execution of chain linq query

旧时模样 提交于 2019-12-22 04:31:35

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/17853363/order-execution-of-chain-linq-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!