C# linq创建嵌套组
生活随笔
收集整理的這篇文章主要介紹了
C# linq创建嵌套组
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
以下示例演示如何在 LINQ 查詢表達式中創建嵌套組。 首先根據學生年級創建每個組,然后根據每個人的姓名進一步細分為小組。
public void QueryNestedGroups() {var queryNestedGroups =from student in studentsgroup student by student.Year into newGroup1from newGroup2 in(from student in newGroup1group student by student.LastName)group newGroup2 by newGroup1.Key;// Three nested foreach loops are required to iterate // over all elements of a grouped group. Hover the mouse // cursor over the iteration variables to see their actual type.foreach (var outerGroup in queryNestedGroups){Console.WriteLine($"DataClass.Student Level = {outerGroup.Key}");foreach (var innerGroup in outerGroup){Console.WriteLine($"\tNames that begin with: {innerGroup.Key}");foreach (var innerGroupElement in innerGroup){Console.WriteLine("\t\t{innerGroupElement.LastName} {innerGroupElement.FirstName}");}}} }請注意,需要使用 3 個嵌套的?foreach?循環來循環訪問嵌套組的內部元素。
轉載于:https://www.cnblogs.com/dehuachenyunfei/p/7777133.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的C# linq创建嵌套组的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python 操作 MySQL 的正确姿
- 下一篇: Java创建多线程的三种方法