Call ILookUp returned from ToLookup to get the IEnumerable

   
 

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

    public static Employee[] GetEmployees() {
        Employee[] actors = new Employee[] {
      new Employee { birthYear = "1964", firstName = "K", lastName = "R" },
      new Employee { birthYear = "1968", firstName = "O", lastName = "W" },
      new Employee { birthYear = "1960", firstName = "J", lastName = "S" },
      new Employee { birthYear = "1965", firstName = "S",lastName = "B" },
    };
       return (actors);
    }
}
public class MyStringifiedNumberComparer : IEqualityComparer<string> {
    public bool Equals(string x, string y) {
        return (Int32.Parse(x) == Int32.Parse(y));
    }

    public int GetHashCode(string obj) {
        return Int32.Parse(obj).ToString().GetHashCode();
    }
}
public class MainClass {
    public static void Main() {

        ILookup<string, string> lookup = Employee.GetEmployees()
          .ToLookup(k => k.birthYear,a => string.Format("{0} {1}", a.firstName, a.lastName),new MyStringifiedNumberComparer());
        IEnumerable<string> actors = lookup["1965"];
        foreach (var actor in actors)
            Console.WriteLine("{0}", actor);
    }
}

    


ToLookup with int and string

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

    public static Employee[] GetEmployees() {
        Employee[] actors = new Employee[] {
       new Employee { birthYear = 1964, firstName = "K", lastName = "R" },
       new Employee { birthYear = 1968, firstName = "O", lastName = "W" },
       new Employee { birthYear = 1960, firstName = "J", lastName = "S" },
       new Employee { birthYear = 1964, firstName = "S", lastName = "B" },
    };
        return (actors);
    }
}
public class MainClass {
    public static void Main() {
        ILookup<int, string> lookup = Employee.GetEmployees()
          .ToLookup(k => k.birthYear,
                    a => string.Format("{0} {1}", a.firstName, a.lastName));

        IEnumerable<string> actors = lookup[1964];
        foreach (var actor in actors)
            Console.WriteLine("{0}", actor);
    }
}

    


ToLookup Demo

   
 

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

public class Employee {
    public string birthYear;
    public string firstName;
    public string lastName;

    public static Employee[] GetActors() {
        Employee[] actors = new Employee[] {
      new Employee { birthYear = "1964", firstName = "K", lastName = "R" },
      new Employee { birthYear = "1968", firstName = "O", lastName = "W" },
      new Employee { birthYear = "1960", firstName = "J", lastName = "S" },
      new Employee { birthYear = "1964", firstName = "S",lastName = "B" },
    };

        return (actors);
    }
}
public class MyStringifiedNumberComparer : IEqualityComparer<string> {
    public bool Equals(string x, string y) {
        return (Int32.Parse(x) == Int32.Parse(y));
    }

    public int GetHashCode(string obj) {
        return Int32.Parse(obj).ToString().GetHashCode();
    }
}
public class MainClass {
    public static void Main() {

        ILookup<string, Employee> lookup = Employee.GetActors()
          .ToLookup(k => k.birthYear, new MyStringifiedNumberComparer());
        IEnumerable<Employee> actors = lookup["1968"];
        foreach (var actor in actors)
            Console.WriteLine("{0} {1}", actor.firstName, actor.lastName);
    }
}

    


calling ToList.

   
 

using System;
using System.Collections.Generic;
using System.Linq;
public class MainClass {
    public static void Main() {

        var numbers = new List<int>() { 1, 2 };

        List<int> timesTen = numbers
          .Select(n => n * 10)
          .ToList();
        numbers.Clear();
        Console.WriteLine(timesTen.Count);
    }
}

    


Convert an ArrayList to an IEnumerable That Can Be Used with the Typical Standard Query Operators

   
 

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(typeof(Employee)));
    }
}

public class MainClass {
    public static void Main() {
        ArrayList employees = Employee.GetEmployeesArrayList();
        Console.WriteLine("The data type of employees is " + employees.GetType());
        var seq = employees.Cast<Employee>();
        Console.WriteLine("The data type of seq is " + seq.GetType());
        var emps = seq.OrderBy(e => e.lastName);
        foreach (Employee emp in emps)
            Console.WriteLine("{0} {1}", emp.firstName, emp.lastName);
    }
}

    


Convert Query result to a List

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

class Employee {
    int _id;
    int _idRole;
    string _lastName;
    string _firstName;

    public int ID {
        get { return _id; }
        set { _id = value; }
    }

    public int IDRole {
        get { return _idRole; }
        set { _idRole = value; }
    }

    public string LastName {
        get { return _lastName; }
        set { _lastName = value; }
    }

    public string FirstName {
        get { return _firstName; }
        set { _firstName = value; }
    }
}
public class MainClass {
    public static void Main() {
        List<Employee> people = new List<Employee> {
              new Employee  { ID = 1, IDRole = 1, LastName = "A", FirstName = "B"},
              new Employee  { ID = 2, IDRole = 2, LastName = "G", FirstName = "T"}
            };
        var query = from p in people
                    where p.LastName.Length == 4
                    select p.LastName;

        List<string> names = query.ToList<string>();
        Console.Write(names);
    }
}

    


Calling the ToList Operator

   
 

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

public class MainClass {
    public static void Main() {
        string[] presidents = {"G", "H", "a", "H", "over", "Jack"};
        List<string> names = presidents.ToList();
        foreach (string name in names)
            Console.WriteLine(name);
    }
}