C# ---ToLookUp
1 理解
数据类型 类似于字典 Dictionary
区别是 TValue 可以有多个, 个人理解 ToLookUp<TKey, List<TValue>>
2. 使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;namespace Study01
{internal class Study05_ToLookup{List<Student> students = new List<Student>();public void BaseToLookup(){for (int i = 0; i < 9; i++){students.Add(new Student() { ClassId = $"001", Name = $"Ly{i}", Age = 20 });}for (int i = 0;i < 9; i++){students.Add(new Student() { ClassId = $"002", Name = $"jo{i}", Age = 22 });}ILookup<string, Student> lookup = students.ToLookup(x=> x.ClassId);foreach (IGrouping<string, Student> student in lookup){Console.WriteLine(student.Key);foreach (Student student2 in student){Console.WriteLine(student2.Name);}}// lookup["002"]foreach (Student item in lookup["002"]){Console.WriteLine(item.Name);} }}public class Student{public string ClassId { get; set; }public string Name { get; set; }public int Age { get; set; }}
}