The deferred execution feature was used in the use of LINQ, so I wrote an article to record >_<
In short, deferred execution is used to save the expressions only when writing a LINQ expression that does not contain mandatory immediate execution commands (such as Count and max. The real execution time is in real use. For specific examples, see:
The result in Row 18 does not save the result, but saves the query itself. If you do not understand the true meaning of "saving the query itself", it is easy to get stuck. The following is my simplified version (You may wish to calculate the output to see if it will be pitted ):
Keng Yi
1 using System; 2 using System.Collections.Generic; 3 using System.Diagnostics; 4 using System.IO; 5 using System.Linq; 6 7 public class Class1 8 { 9 static void Main(string[] args)10 {11 var allData = new List<Tuple<int, string>>() {12 new Tuple<int, string>(0, "Zero"),13 new Tuple<int, string>(1, "and"),14 new Tuple<int, string>(2, "Feng"),15 new Tuple<int, string>(3, "yan")16 };17 18 var t = 0;19 var result = allData.Where(tuple => tuple.Item1 != t);20 21 t = 1;22 foreach (var r in result)23 {24 Console.WriteLine("The " + (t++) + " item: " + r.Item2);25 }26 Console.ReadLine();27 }28 }
The actual output is as follows:
The expected output should not contain "zero". Why does the actual output contain it? The reason is that 19 rows of Results Save the query itself, and this query contains an external variable. The detailed analysis is as follows:
When the foreach statement is executed for the first time, "query" is actually "alldata. Where (tuple => tuple. Item1! = 1) "," zero.
During the second execution of the foreach statement, "query" is actually "alldata. Where (tuple => tuple. Item1! = 2) "," and.
When the foreach statement is executed for the third time, "query" is actually "alldata. Where (tuple => tuple. Item1! = 3) "," Feng.
During the fourth execution of the foreach statement, "query" is actually "alldata. Where (tuple => tuple. Item1! = 4) "," Yan.
The solution to this problem is to force the query to be executed immediately. 19 rows will be changed
var result = allData.Where(tuple => tuple.Item1 != t).ToList();
Now you can get the expected results. For better understanding, you can determine the following output:
using System;using System.Collections.Generic;using System.Diagnostics;using System.IO;using System.Linq;public class Class1{ static void Main(string[] args) { var allData = new List<Tuple<int, string>>() { new Tuple<int, string>(0, "Zero"), new Tuple<int, string>(1, "and"), new Tuple<int, string>(2, "Feng"), new Tuple<int, string>(3, "yan") }; var t = 0; var result = allData.Where(tuple => tuple.Item1 == t); foreach (var r in result) { Console.WriteLine(r.Item2); t += 2; } Console.ReadLine(); }}
Explanation (the font is white ):
When the foreach statement is executed for the first time, "query" is actually "alldata. Where (tuple => tuple. Item1 = 0)" and "zero.
When the foreach statement is executed for the second time, "query" is actually "alldata. Where (tuple => tuple. Item1 = 2)", "and" does not match, "Feng" does
When the foreach statement is executed for the third time, "query" is actually "alldata. where (tuple => tuple. item1 = 4) "," Yan "does not match, alldata has no elements, and ends.
Therefore, the actual output is:
Zero
Feng
Pit2
using System;using System.Collections.Generic;using System.Diagnostics;using System.IO;using System.Linq;public class Class1{ public static void Main(string[] args) { var allData = new List<Tuple<int, string>>() { new Tuple<int, string>(0, "Zero"), new Tuple<int, string>(1, "and"), new Tuple<int, string>(2, "Feng"), new Tuple<int, string>(3, "yan") }; var result = allData.Where(tuple => tuple.Item1 != 0); output(result); allData.RemoveAt(3); output(result); Console.ReadLine(); } private static void output(IEnumerable<Tuple<int, string>> data) { foreach (var d in data) { Console.WriteLine(d.Item2); } Console.WriteLine("************"); }}
The actual output is as follows:
This output indicates that the results are not saved even if the query has been executed once. When the query is used again, the query is re-executed and the current data source is used for execution.
Why? Because query stores the query itself. You may say this is inefficient. Well, this is true in the above scenarios. However, looking at Ms's introduction to LINQ, we will understand why Ms chose this "inefficient" approach:
Language-Integrated Query (LINQ) is a set of features introduced in Visual Studio 2008 that extends powerful query capabilities to the language syntax of C # and Visual Basic. LINQ introduces standard, easily-learned patterns for querying and updating data, and the technology can be extended to support potentially any kind of data store. visual Studio schemdes LINQ provider assemblies that enable the use of LINQ. net Framework collections, SQL Server databases, ADO. net datasets, and XML documents ENTs.
Ms can also use functions similar to SQL statements in non-database scenarios. The same SQL statement should naturally have different results when the database changes. When you want to get the same result, you should not save the query, but save the result, that is, force the query to be executed immediately.
Summary
When using a non-immediately executed LINQ statement, a query is obtained. Each time a query is used, the query is re-executed, and the current data source and external variable values are used for execution, rather than defining the values of these variables during the query. The following are two additional readings. For more information, see:
LINQ and deferred execution
Don't be too lazy-LINQ's lazy evaluation gotchas