1. 程式人生 > >C# ToLookUp 詳解,ToLookUp 和ToDictionary的區別

C# ToLookUp 詳解,ToLookUp 和ToDictionary的區別

    ToDictionary 和ToLookUp 對 物件集合的操作帶來極大的方便,特別是對索引的提供。方便通過 key 來找到相應的鍵值,ToDictionary 轉換成是鍵值對 關係是一 一 對應的關係  且key 值是唯一的不能重複。微軟彷彿意識到ToDictionary 的不足,於是ToLookUp誕生了, ToLookUp 是ToDictionary 的擴充套件版本,個人理解: ToLookUp類似於Dictionary<string,List<string>>,好了廢話不說直接程式碼!

  1. private static void LookUpAndDic()
  2. {
  3.     //建立學生列表
  4.     List<Student> data = getStudents();
  5.     //各個班級學生的分組
  6.     ILookup<string, string> dic = data.ToLookup(item => item.classNo,item=> { return item.stuNo + " 姓名" + item.name; });
  7.     foreach(var item in dic)
  8.     {
  9.         Console.WriteLine("年級編號:"+item.Key);
  10.         foreach(var item1 in item)
  11.         {
  12.             Console.WriteLine("\t\t"+item1);
  13.         }
  14.     }
  15.     Console.WriteLine();
  16.  
  17.     //獲取指定班級的學生
  18.     IEnumerable<string> datas = dic["01"];
  19.     foreach(string item in datas)
  20.     {
  21.         Console.WriteLine("一班的同學:"+item);
  22.      }
  23.     Console.WriteLine();
  24.     //獲取班級個數
  25.     int count = dic.Count;
  26.     Console.WriteLine("獲取班級的個數:"+count);
  27.     Console.WriteLine();
  28.     //判斷某個班級是否存在
  29.     if (dic.Contains("06"))
  30.     {
  31.         Console.WriteLine("06 班已存在!");
  32.     }
  33.     Console.ReadKey();
  34. }
  35.  
  36. public   class Student
  37. {
  38.     /// <summary>
  39.     /// 學生姓名
  40.     /// </summary>
  41.     public string name { get; set; }
  42.     /// <summary>
  43.     /// 年級代號
  44.     /// </summary>
  45.     public string classNo { get; set; }
  46.     /// <summary>
  47.     /// 學生Id
  48.     /// </summary>
  49.     public string stuNo { get; set; }
  50.     }
  51.  }
  52. /// <summary>
  53. /// 獲取所有的students
  54. /// </summary>
  55. /// <returns></returns>
  56. public static List<Student> getStudents()
  57.  {
  58.         return new List<Student>(){
  59.             new Student() { stuNo="0101", classNo="06", name="liu" },
  60.             new Student() { stuNo="0102", classNo="01", name="wang" },
  61.             new Student() { stuNo="0103", classNo="01", name="li" },
  62.             new Student() { stuNo="0104", classNo="02", name="wei" },
  63.             new Student() { stuNo="0105", classNo="02", name="zhao" },
  64.             new Student() { stuNo="0106", classNo="02", name="ma" },
  65.             new Student() { stuNo="0107", classNo="04", name="a" },
  66.             new Student() { stuNo="0108", classNo="03", name="shangGuan" },
  67.             new Student() { stuNo="0109", classNo="03", name="zhang" },
  68.             new Student() { stuNo="0110", classNo="03", name="jiang" },
  69.             new Student() { stuNo="0111", classNo="03", name="wu" },
  70.             new Student() { stuNo="0112", classNo="03", name="test" },
  71.             new Student() { stuNo="0113", classNo="05", name="liu2" },
  72.             };
  73.   }