字典---ToLookup
------------------------------------------------------------------------------Person.cs
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceConsoleApplication1{publicclassPerson{publicstringCountry{get;privateset;}publicstringName{get;privateset;}publicintAge{get;privateset;}publicPerson(stringcountry,stringname,intage){this.Country=country;this.Name=name;this.Age=age;}}}
------------------------------------------------------------------------------主程序
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceConsoleApplication1{classProgram{staticvoidMain(string[]args){//可以用于实现了IEnumerable<T>的所有类//一个键关联多个值List<Person>lp=newList<Person>();lp.Add(newPerson("中国","张飞",40));lp.Add(newPerson("中国","关羽",43));lp.Add(newPerson("中国","刘备",45));lp.Add(newPerson("中国","诸葛亮",24));lp.Add(newPerson("美国","豪威尔",40));lp.Add(newPerson("美国","奥巴马",40));lp.Add(newPerson("朝鲜","金三胖",40));lp.Add(newPerson("印度","印度阿三",40));//传入一个Person对象,返回stringvart=lp.ToLookup(r=>r.Country);//键为Country(国家)foreach(Personitemint["中国"])//找到所有为中国的Person对象{Console.WriteLine(item.Name);}Console.ReadKey();}}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。