共用方式為


Join 子句 (Visual Basic)

將兩個集合合併成單一集合。這項聯結 (Join) 作業是根據相符的索引鍵,使用的是 Equals 運算子。

Join element In collection _
  [ joinClause _ ] 
  [ groupJoinClause ... _ ] 
On key1 Equals key2 [ And key3 Equals key4 [... ]

組件

  • element
    必要項。代表要聯結之集合的控制項變數。

  • collection
    必要項。要與 Join 運算子左邊定義的集合進行合併的集合。Join 子句可以巢狀於另一個 Join 子句或 Group Join 子句中。

  • joinClause
    選擇項。一個或多個用來進一步限定查詢的其他 Join 子句。

  • groupJoinClause
    選擇項。一個或多個用來進一步限定查詢的其他 Group Join 子句。

  • key1Equalskey2
    必要項。識別要聯結之集合的索引鍵。您必須使用 Equals 運算子來比較要聯結之集合中的索引鍵。您可以使用 And 運算子識別多個索引鍵,藉以合併聯結條件 (Join Condition)。key1 必須來自 Join 運算子左邊的集合。key2 必須來自 Join 運算子右邊的集合。

    聯結條件中使用的索引鍵,可以是包含集合中之多個項目的運算式。不過,每個索引鍵運算式只能包含其針對之集合中的項目。

備註

Join 子句會根據要聯結之集合中的相符索引鍵值來合併兩個集合。所產生的集合中,可以包含 Join 運算子左邊所識別集合中的值和 Join 子句所識別集合中的值的任意組合。查詢只會傳回符合 Equals 運算子所指定之條件的結果。這相當於 SQL 中的 INNER JOIN。

您可以在查詢中使用多個 Join 子句,將兩個以上的集合聯結成單一集合。

您可以執行隱含聯結 (Implicit Join) 來合併集合,而不需要使用 Join 子句。其做法是在 From 子句中加入多個 In 子句,然後指定 Where 子句以識別要使用的聯結索引鍵。

您可以使用 Group Join 子句將集合合併成單一階層式集合。這和 SQL 中的 LEFT OUTER JOIN 相似。

範例

下列程式碼範例會執行隱含聯結以將客戶清單及其訂單合併。

Dim customerIDs() = {"ALFKI", "VICTE", "BLAUS", "TRAIH"}

Dim customerList = From cust In customers, custID In customerIDs
                   Where cust.CustomerID = custID
                   Select cust.CompanyName

For Each companyName In customerList
  Console.WriteLine(companyName)
Next

下列程式碼範例會使用 Join 子句聯結兩個集合。

Imports System.Diagnostics
Imports System.Security.Permissions

Public Class JoinSample

  <SecurityPermission(SecurityAction.Demand)>
  Public Sub ListProcesses()
    Dim processDescriptions As New List(Of ProcessDescription)
    processDescriptions.Add(New ProcessDescription With {
                                .ProcessName = "explorer",
                                .Description = "Windows Explorer"})
    processDescriptions.Add(New ProcessDescription With {
                                .ProcessName = "winlogon",
                                .Description = "Windows Logon"})
    processDescriptions.Add(New ProcessDescription With {
                                .ProcessName = "cmd",
                                .Description = "Command Window"})
    processDescriptions.Add(New ProcessDescription With {
                                .ProcessName = "iexplore",
                                .Description = "Internet Explorer"})

    Dim processes = From proc In Process.GetProcesses
                    Join desc In processDescriptions
                      On proc.ProcessName Equals desc.ProcessName
                    Select proc.ProcessName, proc.Id, desc.Description

    For Each proc In processes
      Console.WriteLine("{0} ({1}), {2}",
                        proc.ProcessName, proc.Id, proc.Description)
    Next
  End Sub

End Class

Public Class ProcessDescription
  Public ProcessName As String
  Public Description As String
End Class

這個範例會產生類似如下的輸出:

winlogon (968), Windows Logon

explorer (2424), File Explorer

cmd (5136), Command Window

下列程式碼範例會使用 Join 子句並搭配兩個索引鍵資料行聯結兩個集合。

Imports System.Diagnostics
Imports System.Security.Permissions

Public Class JoinSample2

  <SecurityPermission(SecurityAction.Demand)>
  Public Sub ListProcesses()
    Dim processDescriptions As New List(Of ProcessDescription2)

    ' 8 = Normal priority, 13 = High priority
    processDescriptions.Add(New ProcessDescription2 With {
                                .ProcessName = "explorer",
                                .Description = "Windows Explorer",
                                .Priority = 8})
    processDescriptions.Add(New ProcessDescription2 With {
                                .ProcessName = "winlogon",
                                .Description = "Windows Logon",
                                .Priority = 13})
    processDescriptions.Add(New ProcessDescription2 With {
                                .ProcessName = "cmd",
                                .Description = "Command Window",
                                .Priority = 8})
    processDescriptions.Add(New ProcessDescription2 With {
                                .ProcessName = "iexplore",
                                .Description = "Internet Explorer",
                                .Priority = 8})

    Dim processes = From proc In Process.GetProcesses
                    Join desc In processDescriptions
                      On proc.ProcessName Equals desc.ProcessName And 
                         proc.BasePriority Equals desc.Priority
                    Select proc.ProcessName, proc.Id, desc.Description,
                           desc.Priority

    For Each proc In processes
      Console.WriteLine("{0} ({1}), {2}, Priority = {3}",
                        proc.ProcessName,
                        proc.Id,
                        proc.Description,
                        proc.Priority)
    Next
  End Sub

End Class

Public Class ProcessDescription2
  Public ProcessName As String
  Public Description As String
  Public Priority As Integer
End Class

這個範例會產生類似如下的輸出:

winlogon (968), Windows Logon, Priority = 13

cmd (700), Command Window, Priority = 8

explorer (2424), File Explorer, Priority = 8

請參閱

參考

Select 子句 (Visual Basic)

From 子句 (Visual Basic)

Group Join 子句 (Visual Basic)

Where 子句 (Visual Basic)

概念

Visual Basic 中的 LINQ 簡介

其他資源

查詢 (Visual Basic)