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

    


This entry was posted in LINQ. Bookmark the permalink.