资源预览内容
第1页 / 共4页
第2页 / 共4页
第3页 / 共4页
第4页 / 共4页
亲,该文档总共4页全部预览完了,如果喜欢就下载吧!
资源描述
C#在LINQ中使用GroupBy实现数据分组usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text; namespaceUseGroup/-publicclassStudentpublicstringNameget;set;publicstringCityget;set; classProgramstaticvoidMain(stringargs)/数据源ListStudents=newListnewStudentName=张三,City=北京,newStudentName=李四,City=上海,newStudentName=王五,City=北京,newStudentName=赵六,City=重庆,newStudentName=马七,City=北京,newStudentName=牛八,City=上海,newStudentName=杨九,City=北京,newStudentName=阮十,City=广州,newStudentName=萧十一,City=重庆,newStudentName=伍十二,City=上海,newStudentName=梁十三,City=广州,newStudentName=黄十四,City=北京,newStudentName=刘十五,City=重庆,newStudentName=刀十六,City=上海,newStudentName=元十七,City=重庆,newStudentName=柳十八,City=北京,newStudentName=吴十九,City=广州,newStudentName=魏二十,City=上海; /QueryStudentCity类型为:IEnumerableIGrouping/groupby本质上是实现IGrouping接口的对象/TKey代表分组标识的类型,TElement代表分组中单个数据对象的类型varQueryStudentCity=fromstudentinStudentsgroupstudentbystudent.City; /QueryStudentCity类型为:IGrouping/为了分组现时所有数据,需要嵌套两层循环(两个泛型接口)/外层循环遍历每个组,内层循环遍历每个组中的元素foreach(varCityGroupinQueryStudentCity) Console.WriteLine(n=);Console.WriteLine(在0的学生清单:,CityGroup.Key); intcount=0;foreach(StudentstudentinCityGroup)count+;Console.WriteLine(0:1(2),count,student.Name,student.City);Console.WriteLine(=); Console.ReadKey();
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号