What is delayed loading? The so-called delayed loading means that data loading is performed only when data is actually needed. It can be simply understood that the SQL statement is issued only when it is used for query, and the data is read n times.
What is immediate loading? The so-called immediate loading means that all relevant data is read at a time, rather than being divided into n times.
I believe everyone knows the above concepts. The following is an example:
Classinfo table:
Stuinfo table:
Let me not talk about the relationship between two tables.
I. Next let's look at the delayed Loading Code:
Linqdbdatacontext DB = new linqdbdatacontext (); dB. deferredloadingenabled = true; // true indicates delayed loading. The default value is delayed loading. Therefore, this code can omit string Path = server. mappath (@"~ /Log/log.txt "); streamwriter Sw = file. appendtext (PATH); dB. log = Sw; // create a LINQ to SQL statement var list = from S in dB. classinfo select s; // edit the SQL statement foreach (VAR C in list) {string S1 = string. format ("class ID: {0}, Class Name: {1} <br>", C. classid, C. classname); response. write (S1); foreach (VAR mark in C. stuinfo) {string S2 = string. format ("student ID: {0}, name: {1} <br>", Mark. stunum, Mark. stuname); response. write (S2) ;}} dB. log. flush (); dB. log. close ();
The output result is:
Class ID: 1, class name: T1001
Student ID: s001, name: James
Student ID: s004, name: Wang Wu
Student ID: s006; Name: James
Student ID: s007, name: Guo Xiaoming
Student ID: s009, name: zhangbao
Class ID: 2, Class Name: t1002
Student ID: s002, name: Li Si
Student ID: s003, name: Lili
Student ID: s008; Name: Zhang Jie
Student ID: s011, name: Wang Yan
Student ID: s012, name: Wang Sheng
Class ID: 3, Class Name: t1003
Student ID: s005; Name: Zhao Liu
Student ID: s010, name: Chen Ming
Class ID: 4, Class Name: t1004
Student ID: s013, name: Wang Qian
The log output content is as follows:
SELECT [t0].[ClassID], [t0].[ClassName]FROM [dbo].[ClassInfo] AS [t0]-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.18058SELECT [t0].[StuNum], [t0].[StuName], [t0].[StuAge], [t0].[StuSex], [t0].[ClassID]FROM [dbo].[StuInfo] AS [t0]WHERE [t0].[ClassID] = @p0-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [1]-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.18058SELECT [t0].[StuNum], [t0].[StuName], [t0].[StuAge], [t0].[StuSex], [t0].[ClassID]FROM [dbo].[StuInfo] AS [t0]WHERE [t0].[ClassID] = @p0-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [2]-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.18058SELECT [t0].[StuNum], [t0].[StuName], [t0].[StuAge], [t0].[StuSex], [t0].[ClassID]FROM [dbo].[StuInfo] AS [t0]WHERE [t0].[ClassID] = @p0-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [3]-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.18058SELECT [t0].[StuNum], [t0].[StuName], [t0].[StuAge], [t0].[StuSex], [t0].[ClassID]FROM [dbo].[StuInfo] AS [t0]WHERE [t0].[ClassID] = @p0-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [4]-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.18058
The log shows that the outer foreach traverses classinfo and generates a log for querying classinfo. The in-layer foreach traverses the stuinfo in each classinfo. Because there are four classes, to query the stuinfo in four classinfo files, four logs are generated. In this case, the query is performed only when data is actually needed.
Ii. Next let's take a look at the loaded code:
Linqdbdatacontext DB = new linqdbdatacontext (); DB. deferredloadingenabled = false; // false indicates loading string Path = server. mappath (@"~ /Log/log.txt "); streamwriter Sw = file. appendtext (PATH); dB. log = Sw; // loading mode immediately (the following three codes must be added; otherwise, the student information cannot be found) dataloadoptions DL = new dataloadoptions (); dl. loadwith <classinfo> (S => S. stuinfo); dB. loadoptions = dl; // create a LINQ to SQL statement var list = from S in dB. classinfo select s; // edit the SQL statement foreach (VAR C in list) {string S1 = string. format ("class ID: {0}, Class Name: {1} <br>", C. classid, C. classname); response. write (S1); foreach (VAR mark in C. stuinfo) {string S2 = string. format ("student ID: {0}, name: {1} <br>", Mark. stunum, Mark. stuname); response. write (S2) ;}} dB. log. flush (); dB. log. close ();
The output result is the same as the preceding one, but let's look at the log information:
SELECT [t0].[ClassID], [t0].[ClassName], [t1].[StuNum], [t1].[StuName], [t1].[StuAge], [t1].[StuSex], [t1].[ClassID] AS [ClassID2], ( SELECT COUNT(*) FROM [dbo].[StuInfo] AS [t2] WHERE [t2].[ClassID] = [t0].[ClassID] ) AS [value]FROM [dbo].[ClassInfo] AS [t0]LEFT OUTER JOIN [dbo].[StuInfo] AS [t1] ON [t1].[ClassID] = [t0].[ClassID]ORDER BY [t0].[ClassID], [t1].[StuNum]-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.18058
It is obvious that loading is performed immediately, and the query is read at one time.
Latency loading of LINQ