Calling the First ToDictionary Prototype

image_pdfimage_print
   
 

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 MainClass {
    public static void Main() {
        Dictionary<int, Employee> eDictionary = Employee.GetEmployeesArray().ToDictionary(k => k.id);
        Employee e = eDictionary[2];
        Console.WriteLine("Employee whose id == 2 is {0} {1}", e.firstName, e.lastName);
    }
}

    


Second ToDictionary

image_pdfimage_print
   
 

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

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

    public static Employee2[] GetEmployeesArray() {
        return ((Employee2[])GetEmployeesArrayList().ToArray(typeof(Employee2)));
    }
}
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() {
        Dictionary<string, Employee2> eDictionary = Employee2.GetEmployeesArray().ToDictionary(k => k.id, new MyStringifiedNumberComparer());
        Employee2 e = eDictionary["2"];
        Console.WriteLine("Employee whose id == "2" : {0} {1}", e.firstName, e.lastName);
        e = eDictionary["000002"];
        Console.WriteLine("Employee whose id == "000002" : {0} {1}",e.firstName, e.lastName);
    }
}

    


Convert query result to an Array

image_pdfimage_print

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 people = new List {
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;

string[] names = query.ToArray();

for (int i = 0; i < names.Length; i++) Console.WriteLine(names[i]); } } [/csharp]

generates an array of double values by first using a query expression with orderby

image_pdfimage_print

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

public class MainClass {
public static void Main() {
double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 };
var sortedDoubles =
from d in doubles
orderby d descending
select d;
var doublesArray = sortedDoubles.ToArray();

Console.WriteLine(“Every other double from highest to lowest:”);
for (int d = 0; d < doublesArray.Length; d += 2) { Console.WriteLine(doublesArray[d]); } } } [/csharp]

To List: convert query result to list

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

public class MainClass {
    public static void Main() {
        string[] words = { "ch", "a", "b" };
        var sortedWords =
            from w in words
            orderby w
            select w;
        var wordList = sortedWords.ToList();
        Console.WriteLine("The sorted word list:");
        foreach (var w in wordList) {
            Console.WriteLine(w);
        }
    }
}

    


Convert query to array with ToArray

image_pdfimage_print
   
 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
class Person {
    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; }
    }
}
class Program {
    static void Main(string[] args) {
        List<Person> people = new List<Person> {
               new Person { ID = 1, IDRole = 1, LastName = "A", FirstName = "B"},
               new Person { ID = 2, IDRole = 2, LastName = "G", FirstName = "T"}
            };
        var query = people
                    .Where(p => p.ID == 1)
                    .Select(p => new { p.FirstName, p.LastName })
                    .ToArray();
        people[0].FirstName = "F";
    }
}

    


Use Conversion Operators ToArray

image_pdfimage_print

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

public class MainClass {
public static void Main() {
double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 };
var sortedDoubles =
from d in doubles
orderby d descending
select d;
var doublesArray = sortedDoubles.ToArray();

Console.WriteLine(“Every other double from highest to lowest:”);
for (int d = 0; d < doublesArray.Length; d += 2) { Console.WriteLine(doublesArray[d]); } } } [/csharp]