This article describes the deferred execution of LINQ and Query Non-generic collections.
Deferred execution
From the perspective of LINQ query process, there are new types, collection construction, release, feeling inefficient, but the delayed execution mechanism of LINQ will greatly improve the general operation of loading the entire data source caused by the inefficient situation.
Example:
static void Main()
{
int[] collection = { 10, 11, 12, 13, 14 };
Console.WriteLine("Begin query collection:" + DateTime.Now);
var result = from i in collection
select DoubleInt(i);
Console.WriteLine("End query collection & Begin output:" + DateTime.Now);
foreach (var i in result)
{
Console.WriteLine("Result:" + i + " Time:" + DateTime.Now.ToString());
}
Console.WriteLine("End output:" + DateTime.Now);
}
static int DoubleInt(int i)
{
Console.WriteLine("The parameter is:" + i);
Thread.Sleep(1000);
return i * 2;
}
The output is:// Begin query collection:2008-9-12 22:41:50
// End query collection & Begin output:2008-9-12 22:41:50
// The parameter is:10
// Result:20 Time:2008-9-12 22:41:51
// The parameter is:11
// Result:22 Time:2008-9-12 22:41:52
// The parameter is:12
// Result:24 Time:2008-9-12 22:41:53
// The parameter is:13
// Result:26 Time:2008-9-12 22:41:54
// The parameter is:14
// Result:28 Time:2008-9-12 22:41:55
// End output:2008-9-12 22:41:55