SelectMany Prototype: with ? operator

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
public static void Main() {
string[] presidents = {“A”, “Art”, “Buch”, “Bush”, “Carter”, “land”};

IEnumerable chars = presidents
.SelectMany((p, i) => i < 5 ? p.ToArray() : new char[] { }); foreach (char ch in chars) Console.WriteLine(ch); } } [/csharp]

SelectMany Prototype

   
 

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
    public static void Main() {
        string[] presidents = {"Ad", "Art", "Buch", "Bush", "Car", "land"};

        IEnumerable<char> chars = presidents.SelectMany(p => p.ToArray());

        foreach (char ch in chars)
            Console.WriteLine(ch);

    }
}

    


A More Complex Example of the First SelectMany

   
 
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class Employee {
    public int id;
    public string firstName;
    public string lastName;

    public static ArrayList GetEmployeesArrayList() {
        ArrayList al = new ArrayList();
        al.Add(new Employee { id = 1, firstName = "J", lastName = "R" });
        al.Add(new Employee { id = 2, firstName = "W", lastName = "G" });
        al.Add(new Employee { id = 3, firstName = "A", lastName = "H" });
        al.Add(new Employee { id = 4, firstName = "D", lastName = "L" });
        al.Add(new Employee { id = 101, firstName = "K", lastName = "F" });
        return (al);
    }

    public static Employee[] GetEmployeesArray() {
        return ((Employee[])GetEmployeesArrayList().ToArray());
    }
}
public class EmployeeOptionEntry {
    public int id;
    public long optionsCount;
    public DateTime dateAwarded;

    public static EmployeeOptionEntry[] GetEmployeeOptionEntries() {
        EmployeeOptionEntry[] empOptions = new EmployeeOptionEntry[] {
      new EmployeeOptionEntry {
        id = 1,
        optionsCount = 2,
         dateAwarded = DateTime.Parse("1999/12/31") },
       new EmployeeOptionEntry {
        id = 101,
        optionsCount = 2,
        dateAwarded = DateTime.Parse("1998/12/31") }
    };

        return (empOptions);
    }
}


public class MainClass {
    public static void Main() {
        Employee[] employees = Employee.GetEmployeesArray();
        EmployeeOptionEntry[] empOptions = EmployeeOptionEntry.GetEmployeeOptionEntries();

        var employeeOptions = employees

         .SelectMany(e => empOptions
                               .Where(eo => eo.id == e.id)
                               .Select(eo => new {
                                   id = eo.id,
                                   optionsCount = eo.optionsCount
                               }));

        foreach (var item in employeeOptions)
            Console.WriteLine(item);

    }
}

    


Returning a List

   
 

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
    public static void Main() {
        int[] intArray = new int[] { 1, 2, 3 };
        List<int> ints = intArray.Select(i => i).ToList();

        foreach (int i in ints)
            Console.WriteLine(i);

        intArray[0] = 5;

        foreach (int i in ints)
            Console.WriteLine(i);
    }
}

    


Query with Intentional Exception Deferred Until Enumeration

   
 

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
    public static void Main() {
        string[] strings = { "one", "two", null, "three" };

        Console.WriteLine("Before Where() is called.");
        IEnumerable<string> ieStrings = strings.Where(s => s.Length == 3);
        Console.WriteLine("After Where() is called.");

        foreach (string s in ieStrings) {
            Console.WriteLine("Processing " + s);
        }

    }
}