Query Reuse

Query
public void Linq101()
{
// Deferred execution lets us define a query once
// and then reuse it later after data changes.

int[] numbers = new int[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNumbers =
from n in numbers
where n <= 3
select n;

Console.WriteLine("First run numbers <= 3:");
foreach (int n in lowNumbers)
{
Console.WriteLine(n);
}

for (int i = 0; i < 10; i++)
{
numbers[i] = -numbers[i];
}

// During this second run, the same query object,
// lowNumbers, will be iterating over the new state
// of numbers[], producing different results:
Console.WriteLine("Second run numbers <= 3:");
foreach (int n in lowNumbers)
{
Console.WriteLine(n);
}
}
Lambda Expression
public void Linq101()
{

// Deferred execution lets us define a query once
// and then reuse it later in various ways.
int[] numbers = new int[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNumbers = numbers.Where(num => num <= 3);

Console.WriteLine("First run numbers <= 3:");
foreach (int n in lowNumbers)
{
Console.WriteLine(n);
}

// Query the original query.
var lowEvenNumbers = lowNumbers.Where(p => p % 2 == 0);

Console.WriteLine("Run lowEvenNumbers query:");
foreach (int n in lowEvenNumbers)
{
Console.WriteLine(n);
}


// Modify the source data.
for (int i = 0; i < 10; i++)
{
numbers[i] = -numbers[i];
}

// During this second run, the same query object,
// lowNumbers, will be iterating over the new state
// of numbers[], producing different results:
Console.WriteLine("Second run numbers <= 3:");
foreach (int n in lowNumbers)
{
Console.WriteLine(n);
}
}
Output
First run numbers <= 3:
1
3
2
0
Second run numbers <= 3:
-5
-4
-1
-3
-9
-8
-6
-7
-2
0
Related Tutorial