首页 > 其他分享 >What does Include() do in LINQ?

What does Include() do in LINQ?

时间:2022-08-22 18:13:01浏览次数:117  
标签:do What loading Customers related entity Include data

What does Include() do in LINQ?

问题

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?

 

回答1

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.

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

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

SELECT * FROM Customers;

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

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

 

回答2

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


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) with Reference(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.

 

标签:do,What,loading,Customers,related,entity,Include,data
From: https://www.cnblogs.com/chucklu/p/16613785.html

相关文章

  • docker--从仓库下载镜像到推送自己的项目到仓库
     dockerpull用户/仓库:标签#-it以交互模式启动一个容器#--name命名容器#-d后台运行容器,并返回容器IDdockerrun-it--name定义容器名-d镜像名:标......
  • docker jenkins centos7.9安装
    1、搜索jenkins镜像dockersearchjenkins---------------------NAMEDESCRIPTIONSTARSOFFICIALAUTOMATED......
  • IPQ6018 wallys OpenWrt 2.4/5G dual bands 802.11ax Indoor Aluminium alloy mater
    QCN9074WiFi6ECardOpenWRT,IPQ6010,802.11ax,2x22.4G&5GQCN9074WiFiCardIPQ6010,802.11ax,2x22.4G&5G,SupportOpenWRT  MT7915/MT7975/IPQ6000/IP......
  • Clickhouse windowFunnel函数使用
    --官方文档https://clickhouse.com/docs/zh/sql-reference/aggregate-functions/parametric-functions/#function-sequencecount对于事件进行连续跟踪分析能力,适用漏斗......
  • winform开发windows服务过程简要回顾
    总结下,winform开发windows服务全过程 ;windows服务的代码中,不能有MessageBox.Show()等winform的控件引用。可以使用写文本日志的方法调试;1、添加服务引用,输入webservice......
  • CF1715D 2+ doors 题解
    个人认为这道D比C要简单。思路因为题目中每个条件限制为$a_i\mida_j=x$,并且题目中还提到\(x<2^{30}\),我们考虑将\(x\)转换成二进制的方式表示,枚举\(x\)的......
  • CodeForces-1715D 2+ doors
    2+doors贪心位与位之间互不一向,因此考虑每个位进行考虑就行因为是或的关系,先考虑\(0\)的情况,如果出现\(0\),则两个数字的该位必然是\(0\)如果是\(1\)的情况,就考......
  • Windows环境下IDEA配置SSH Key访问Gitlab源代码仓库
    使用Gitlab管理项目时,project在创建的时候只能选择“Public”公开状态,Private和Internal私有模式下不能使用http方式进行连接。(ssh方式在三种模式下都可以)。使用http方式直......
  • docker快速搭建redis集群
    采用三台主节点Docker部署,均采用默认端口,每台机器一台redis,可以视情况自行修改#清理环境,所有节点执行!注意会删除数据rm-rf/opt/redis/clustermkdir-p/opt/redi......
  • docker mysql问题
    启动出错[root@main~]#dockerlogsmysql-first2022-08-2202:50:33+00:00[Note][Entrypoint]:EntrypointscriptforMySQLServer8.0.30-1.el8started.2022-0......