Shows how queries can be reused : Deferred Query « LINQ « C# / C Sharp






Shows how queries can be reused

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class MainClass {
    public static void Main() {
        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];
        }

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

 








Related examples in the same category

1.Deferred Query Execution: shows how query execution is deferred until the query is enumerated at a foreach statement.
2.How queries can be executed immediately with operators such as ToList().
3.Deferred Query Execution
4.Immediate Query Execution
5.A deferred execution query is reevaluated when you reenumerate:
6.most query operators is that they execute not when constructed, but when enumerated