Immediate Query Execution : Deferred Query « LINQ « C# / CSharp Tutorial






using System;
using System.Linq;

class MainClass {
    static double Square(double n) {
        Console.WriteLine("Computing Square(" + n + ")...");
        return Math.Pow(n, 2);
    }

    public static void Main() {
        int[] numbers = { 1, 2, 3 };
        var query = from n in numbers select Square(n);
        foreach (var n in query.ToList())
            Console.WriteLine(n);
    }
}








22.76.Deferred Query
22.76.1.Deferred Query Execution: shows how query execution is deferred until the query is enumerated at a foreach statement.
22.76.2.How queries can be executed immediately with operators such as ToList().
22.76.3.Shows how queries can be reused
22.76.4.Deferred Query Execution
22.76.5.Immediate Query Execution
22.76.6.A deferred execution query is reevaluated when you reenumerate:
22.76.7.most query operators is that they execute not when constructed, but when enumerated