SQL Include() 在 LINQ 中有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26661771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does Include() do in LINQ?
提问by C.J.
I tried to do a lot of research but I'm more of a db guy - so even the explanation in the MSDN doesn't make any sense to me. Can anyone please explain, and provide some examples on what Include()
statement does in the term of SQL
query?
我试图做很多研究,但我更像是一个数据库人 - 所以即使 MSDN 中的解释对我来说也没有任何意义。任何人都可以解释一下,并提供一些关于查询Include()
术语中的语句的示例SQL
吗?
回答by Yuck
Let's say for instance you want to get a list of all your customers:
例如,假设您想获取所有客户的列表:
var customers = context.Customers.ToList();
And let's assume that each Customer
object has a reference to its set of Orders
, and that each Order
has references to LineItems
which may also reference a Product
.
让我们假设每个Customer
对象都有一个对它的集合的引用Orders
,并且每个对象都有一个引用,这些Order
引用LineItems
也可能引用一个Product
。
As you can see, selecting a top-level object with many related entities could result in a query that needs to pull in data from many sources. As a performance measure, Include()
allows you to indicate which related entities should be read from the database as part of the same query.
如您所见,选择具有许多相关实体的顶级对象可能会导致查询需要从许多来源中提取数据。作为一种性能度量,Include()
允许您指示应从数据库中读取哪些相关实体作为同一查询的一部分。
Using the same example, this might bring in all of the related order headers, but none of the other records:
使用相同的示例,这可能会引入所有相关的订单标题,但不会引入任何其他记录:
var customersWithOrderDetail = context.Customers.Include("Orders").ToList();
As a final point since you asked for SQL, the first statement without Include()
could generate a simple statement:
作为您要求 SQL 后的最后一点,没有的第一个语句Include()
可以生成一个简单的语句:
SELECT * FROM Customers;
The final statement which calls Include("Orders")
may look like this:
调用的最终语句Include("Orders")
可能如下所示:
SELECT *
FROM Customers JOIN Orders ON Customers.Id = Orders.CustomerId;
回答by Minoosha
I just wanted to add that "Include" is part of eager loading. It is described in Entity Framework 6 tutorial by Microsoft. Here is the link: https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application
我只是想补充一点,“包含”是急切加载的一部分。Microsoft 在 Entity Framework 6 教程中对此进行了描述。这是链接:https: //docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/reading-related-data-with-the -entity-framework-in-an-asp-net-mvc-application
Excerpt from the linked page:
链接页面的摘录:
Here are several ways that the Entity Framework can load related data into the navigation properties of an entity:
Lazy loading.When the entity is first read, related data isn't retrieved. However, the first time you attempt to access a navigation property, the data required for that navigation property is automatically retrieved. This results in multiple queries sent to the database — one for the entity itself and one each time that related data for the entity must be retrieved. The DbContext class enables lazy loading by default.
Eager loading.When the entity is read, related data is retrieved along with it. This typically results in a single join query that retrieves all of the data that's needed. You specify eager loading by using the
Include
method.Explicit loading.This is similar to lazy loading, except that you explicitly retrieve the related data in code; it doesn't happen automatically when you access a navigation property. You load related data manually by getting the object state manager entry for an entity and calling the Collection.Load method for collections or the Reference.Load method for properties that hold a single entity. (In the following example, if you wanted to load the Administrator navigation property, you'd replace
Collection(x => x.Courses)
withReference(x => x.Administrator)
.) Typically you'd use explicit loading only when you've turned lazy loading off.Because they don't immediately retrieve the property values, lazy loading and explicit loading are also both known as deferred loading.
实体框架可以通过以下几种方式将相关数据加载到实体的导航属性中:
懒加载。首次读取实体时,不会检索相关数据。但是,第一次尝试访问导航属性时,会自动检索该导航属性所需的数据。这会导致向数据库发送多个查询——一个针对实体本身,一个针对实体的相关数据每次必须检索时。DbContext 类默认启用延迟加载。
急切加载。当实体被读取时,相关数据也随之被检索。这通常会导致一个单一的连接查询,检索所有需要的数据。您可以使用
Include
方法指定预先加载。显式加载。这类似于延迟加载,只是您在代码中显式检索相关数据;当您访问导航属性时,它不会自动发生。您可以通过获取实体的对象状态管理器条目并为集合调用 Collection.Load 方法或为包含单个实体的属性调用 Reference.Load 方法来手动加载相关数据。(在下面的示例中,如果您想加载 Administrator 导航属性,您将替换
Collection(x => x.Courses)
为Reference(x => x.Administrator)
。)通常,您仅在关闭延迟加载时才使用显式加载。因为它们不会立即检索属性值,所以延迟加载和显式加载也都称为延迟加载。
回答by robkrueger
Think of it as enforcing Eager-Loading in a scenario where you sub-items would otherwise be lazy-loading.
将其视为在您的子项将是延迟加载的情况下强制执行 Eager-Loading。
The Query EF is sending to the database will yield a larger result at first, but on access no follow-up queries will be made when accessing the included items.
查询 EF 发送到数据库首先会产生更大的结果,但在访问包含的项目时不会进行后续查询。
On the other hand, without it, EF would execute separte queries later, when you first access the sub-items.
另一方面,如果没有它,当您第一次访问子项时,EF 将在稍后执行单独的查询。