链linq查询的订单执行 [英] Order execution of chain linq query

查看:53
本文介绍了链linq查询的订单执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码有区别吗?

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

还有

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

这是延期查询,我不知道,它是否等效?还是在第二种情况下,在Include之后执行where?

谢谢.

解决方案

对于选择之前的EF顺序而言,没有关系. LINQ查询将转换为SQL查询并运行,并且SQL查询优化器不在乎原始查询的顺序.

正如Patryk所指出的,当以下语句修改查询的结构时,顺序对于Include尤为重要,但是where子句却不能这样做.

在其他LINQ查询中,LINQ-to-Objects的顺序非常重要,因为查询没有按照SQL的方式进行重新优化,并且只是从上到下进行简单处理,并且某些LINQ方法需要以前的方法才能运行完成并处理结果,然后再处理第一个元素(例如OrderBy).

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

这篇关于链linq查询的订单执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆