Csharp/CSharp Tutorial/LINQ/ToDictionary

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Calling the First ToDictionary Prototype

<source lang="csharp">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);
   }

}</source>

Convert query to Dictionary

<source lang="csharp">using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Linq; public class MainClass{

  public static void Main(){
           var q = from m in typeof(int).GetMethods()
                   group m by m.Name into gb
                   select gb;
           Dictionary<string, int> d = q.ToDictionary(k => k.Key, k => k.Count());
  }

}</source>

Second ToDictionary

<source lang="csharp">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);
   }

}</source>

ToDictionary: convert query result to Dictionary

<source lang="csharp">using System; using System.Collections.Generic; using System.Linq; using System.Text; public class MainClass {

   public static void Main() {
       var scoreRecords = new[] { new {Name = "A", Score = 50},
                               new {Name = "B" , Score = 40},
                               new {Name = "C", Score = 45}
                             };
       var scoreRecordsDict = scoreRecords.ToDictionary(sr => sr.Name);
       Console.WriteLine("Bob"s score: {0}", scoreRecordsDict["Bob"]);
   }

}</source>

ToDictionary for an object list

<source lang="csharp">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, string> eDictionary = Employee.GetEmployeesArray().ToDictionary(k => k.id, i => string.Format("{0} {1}",i.firstName, i.lastName));
       string name = eDictionary[2];
       Console.WriteLine("Employee whose id == 2 is {0}", name);
   }

}</source>

ToDictionary with IEqualityComparer

<source lang="csharp">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, string> eDictionary = Employee2.GetEmployeesArray()
         .ToDictionary(k => k.id,i => string.Format("{0} {1}",i.firstName, i.lastName),new MyStringifiedNumberComparer());
       string name = eDictionary["2"];
       Console.WriteLine("Employee whose id == \"2\" : {0}", name);
       name = eDictionary["000002"];
       Console.WriteLine("Employee whose id == \"000002\" : {0}", name);
   }

}</source>