首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

LINQ 學習筆記 (六) Group 與 Join 進階 (四) GroupJoin 總結

     編輯:關於C語言

GroupJoin 群組聯接

GroupJoin 將外部序列與內部序列依鍵值關聯,結果為外部元素對應的內部元素群組。方法原型:

public static IEnumerable GroupJoin(
    this IEnumerable outer,
    IEnumerable inner,
    Func outerKeySelector,
    Func innerKeySelector,
    Func, TResult> resultSelector)

GroupJoin 等價操作

var result = from item in
             (from student in DataSource.Students
              join score in DataSource.Scores on student.StudentID equals score.StudentID
              select new { StudentName = student.Name, CourseID = score.CourseID, Value = score.Value })
             group item by item.StudentName into Group
             select new { StudentName = Group.Key, Group = Group };

此操作結合了 Join 與 GroupBy,產生階層式結果:每位學生對應其所有成績群組。

系列總結

至此,LINQ 對應 SQL 的核心操作(投影、篩選、排序、分組、聯接、集合運算、聚合)已基本介紹完畢。組合這些操作可實現複雜查詢需求。建議實際動手練習,並參考 Enumerable 原始碼深入理解實作細節。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved