Transformation: link two array.

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

public class MainClass {
    public static void Main() {

        int[] numbers = { 5, 4, 1};
        string[] strings = { "zero", "one", "two"};

        var textNums =
            from n in numbers
            select strings[n];

        Console.WriteLine("Number strings:");
        foreach (var s in textNums) {
            Console.WriteLine(s);
        }
    }
}

    


Select with Anonymous Types: prints uppercase and lowercase versions of each string in an input array

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

public class MainClass {
    public static void Main() {

        string[] words = { "Abc", "aBc", "AAA" };

        var upperLowerWords =
            from w in words
            select new { Upper = w.ToUpper(), Lower = w.ToLower() };

        foreach (var ul in upperLowerWords) {
            Console.WriteLine("Uppercase: {0}, Lowercase: {1}", ul.Upper, ul.Lower);
        }
    }
}

    


Select with Anonymous Types: iterates over each element to print the element's name

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

public class MainClass {
    public static void Main() {

        int[] numbers = { 5, 4, 1};
        string[] strings = { "zero", "one", "two"};

        var digitOddEvens =
            from n in numbers
            select new { Digit = strings[n], Even = (n % 2 == 0) };

        foreach (var d in digitOddEvens) {
            Console.WriteLine("The digit {0} is {1}.", d.Digit, d.Even ? "even" : "odd");
        }
    }
}

    


Query Reuse

image_pdfimage_print

using System;
using System.Linq;

static class QueryReuse {
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)
Console.WriteLine(n);

for (int i = 0; i < numbers.Length; i++) numbers[i] = numbers[i] + 10; foreach (var n in query) Console.WriteLine(n); } } [/csharp]

An Example Calling the Reverse Operator

image_pdfimage_print
   
 

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
    public static void Main() {
        string[] presidents = {"lore", "Truman", "Tailer", "Van", "Washington", "Wilson"};

        IEnumerable<string> items = presidents.Reverse();

        foreach (string item in items)
            Console.WriteLine(item);
    }
}