Execut query immediately with operators such as ToList() in CSharp

Description

The following code shows how to execut query immediately with operators such as ToList().

Example


 //  w w  w.  j  a v  a2 s . c o m
 
 
  
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 };

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

The code above generates the following result.





















Home »
  C# Tutorial »
    LINQ »




Operator
Select
Where
OrderBy
Group
Join
Let
LINQ