LINQ中的Include()有什么作用? [英] What does Include() do in LINQ?

查看:1636
本文介绍了LINQ中的Include()有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试进行了大量研究,但我更像是数据库专家-因此,即使MSDN中的解释对我也没有任何意义.有人可以解释一下,并提供一些示例说明SQL查询中的Include()语句吗?

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?

推荐答案

例如,假设您要获取所有客户的列表:

Let's say for instance you want to get a list of all your customers:

var customers = context.Customers.ToList();

并假定每个Customer对象都有对其Orders集的引用,并且每个Order都具有对LineItems的引用,而该LineItems也可以引用Product.

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.

如您所见,选择具有许多相关实体的顶级对象可能会导致查询需要从许多来源提取数据.作为一项性能指标,Include()允许您指示应在同一查询中从数据库中读取哪些相关实体.

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.

使用相同的示例,这可能会带来所有相关的订单标题,但没有其他记录:

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();

自从您要求使用SQL以来,最后一点是,没有Include()的第一条语句可以生成一个简单的语句:

As a final point since you asked for SQL, the first statement without Include() could generate a simple statement:

SELECT * FROM Customers;

调用Include("Orders")的最终语句可能看起来像这样:

The final statement which calls Include("Orders") may look like this:

SELECT *
FROM Customers JOIN Orders ON Customers.Id = Orders.CustomerId;

这篇关于LINQ中的Include()有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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