Query
public void Linq100()
{
// Methods like ToList() cause the query to be
// executed immediately, caching the results.
int[] numbers = new int[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
int i = 0;
var q = (
from n in numbers
select ++i)
.ToList();
// The local variable i has already been fully
// incremented before we iterate the results:
foreach (var v in q)
{
Console.WriteLine("v = {0}, i = {1}", v, i);
}
}
Lambda Expression
public void Linq100()
{
// Methods like ToList(), Max(), and Count() cause the query to be
// executed immediately.
int[] numbers = new int[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
int i = 0;
var immediateQuery = (numbers.Select(num => ++i)).ToList();
Console.WriteLine("The current value of i is {0}", i); //i has been incremented
foreach (var item in immediateQuery)
{
Console.WriteLine("v = {0}, i = {1}", item, i);
}
}
Output
v = 1, i = 10
v = 2, i = 10
v = 3, i = 10
v = 4, i = 10
v = 5, i = 10
v = 6, i = 10
v = 7, i = 10
v = 8, i = 10
v = 9, i = 10
v = 10, i = 10