Deferred Query Execution: shows how query execution is deferred until the query is enumerated at a foreach statement. : Deferred Query « LINQ « C# / C Sharp






Deferred Query Execution: shows how query execution is deferred until the query is enumerated at a foreach statement.

 
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 };

        int i = 0;
        var q =
            from n in numbers
            select ++i;
        foreach (var v in q) {
            Console.WriteLine("v = {0}, i = {1}", v, i);
        }
    }
}

 








Related examples in the same category

1.How queries can be executed immediately with operators such as ToList().
2.Shows how queries can be reused
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