using System;
using System.Linq; namespace Linq101
{
class QueryExecution
{
/// <summary>
/// The following sample shows how query execution is deferred until the query is enumerated at a foreach statement.
/// </summary>
public void Linq99()
{
int[] numbers = new int[] { , , , , , , , , , }; int i = ; //延迟执行
var q = from n in numbers select ++i; foreach (int n in q)
{
Console.WriteLine("n={0},i={1}", n, i);
}
} /// <summary>
/// The following sample shows how queries can be executed immediately with operators such as ToList().
/// </summary>
public void Linq100()
{
int[] numbers = new int[] { , , , , , , , , , }; int i = ; //ToList->立即执行
var q = (from n in numbers select ++i).ToList(); foreach (int n in q)
{
Console.WriteLine("n={0},i={1}", n, i);
}
} /// <summary>
/// The following sample shows how, because of deferred execution, queries can be used again after data changes and will then operate on the new data.
/// </summary>
public void Linq101()
{
int[] numbers = { , , , , , , , , , }; //可重用
var lowNumbers = from n in numbers
where n <=
select n; Console.WriteLine("First run numbers <=3:");
foreach (int number in lowNumbers)
{
Console.WriteLine(number);
} lowNumbers = (from n in numbers select -n).ToArray(); Console.WriteLine("Second run numbers <=3:");
foreach (int number in lowNumbers)
{
Console.WriteLine(number);
}
}
}
}