Visual Basic 提供 Join
和 Group Join
查詢子句,可讓您根據集合之間的一般值來結合多個集合的內容。 這些值稱為 索引鍵 值。 熟悉關係資料庫概念的開發人員會識別 Join
子句為 INNER JOIN,而 Group Join
子句實際上是 LEFT OUTER JOIN。
在本主題中,範例展示了使用 Join
和 Group Join
查詢子句來結合數據的幾種方法。
建立專案並新增範例數據
建立包含範例數據和類型的專案
若要執行本主題中的範例,請開啟Visual Studio並新增Visual Basic控制台應用程式專案。 請雙擊 Visual Basic 所建立的 Module1.vb 檔案。
本主題的範例使用
Person
和Pet
類型及來自以下程式代碼範例的數據。 將此程式代碼複製到 Visual Basic 所建立的預設Module1
模組。Private _people As List(Of Person) Private _pets As List(Of Pet) Function GetPeople() As List(Of Person) If _people Is Nothing Then CreateLists() Return _people End Function Function GetPets(ByVal people As List(Of Person)) As List(Of Pet) If _pets Is Nothing Then CreateLists() Return _pets End Function Private Sub CreateLists() Dim pers As Person _people = New List(Of Person) _pets = New List(Of Pet) pers = New Person With {.FirstName = "Magnus", .LastName = "Hedlund"} _people.Add(pers) _pets.Add(New Pet With {.Name = "Daisy", .Owner = pers}) pers = New Person With {.FirstName = "Terry", .LastName = "Adams"} _people.Add(pers) _pets.Add(New Pet With {.Name = "Barley", .Owner = pers}) _pets.Add(New Pet With {.Name = "Boots", .Owner = pers}) _pets.Add(New Pet With {.Name = "Blue Moon", .Owner = pers}) pers = New Person With {.FirstName = "Charlotte", .LastName = "Weiss"} _people.Add(pers) _pets.Add(New Pet With {.Name = "Whiskers", .Owner = pers}) ' Add a person with no pets for the sake of Join examples. _people.Add(New Person With {.FirstName = "Arlene", .LastName = "Huff"}) pers = New Person With {.FirstName = "Don", .LastName = "Hall"} ' Do not add person to people list for the sake of Join examples. _pets.Add(New Pet With {.Name = "Spot", .Owner = pers}) ' Add a pet with no owner for the sake of Join examples. _pets.Add(New Pet With {.Name = "Unknown", .Owner = New Person With {.FirstName = String.Empty, .LastName = String.Empty}}) End Sub
Class Person Public Property FirstName As String Public Property LastName As String End Class Class Pet Public Property Name As String Public Property Owner As Person End Class
使用 Join 子句執行內部聯結
INNER JOIN 會結合來自兩個集合的數據。 包含指定鍵值相符的項目。 任何集合中沒有匹配項目的項目都會被排除。
在 Visual Basic 中,LINQ 提供兩個選項來執行 INNER JOIN:隱含聯結和明確聯結。
隱含聯結會指定在 From
子句中要連結的集合,並在 Where
子句中識別相符的索引鍵欄位。 Visual Basic 會根據指定的鍵欄位隱式聯結這兩個集合。
當您希望明確指定聯結時,可以使用 Join
子句來指定要在聯結中使用的索引鍵欄位。 在此情況下, Where
子句仍可用來篩選查詢結果。
若要使用 Join 子句進行內部連接
將下列程式代碼新增至專案中的
Module1
模組,以查看隱含和明確內部聯結的範例。Sub InnerJoinExample() ' Create two lists. Dim people = GetPeople() Dim pets = GetPets(people) ' Implicit Join. Dim petOwners = From pers In people, pet In pets Where pet.Owner Is pers Select pers.FirstName, PetName = pet.Name ' Display grouped results. Dim output As New System.Text.StringBuilder For Each pers In petOwners output.AppendFormat( pers.FirstName & ":" & vbTab & pers.PetName & vbCrLf) Next Console.WriteLine(output) ' Explicit Join. Dim petOwnersJoin = From pers In people Join pet In pets On pet.Owner Equals pers Select pers.FirstName, PetName = pet.Name ' Display grouped results. output = New System.Text.StringBuilder() For Each pers In petOwnersJoin output.AppendFormat( pers.FirstName & ":" & vbTab & pers.PetName & vbCrLf) Next Console.WriteLine(output) ' Both queries produce the following output: ' ' Magnus: Daisy ' Terry: Barley ' Terry: Boots ' Terry: Blue Moon ' Charlotte: Whiskers End Sub
使用群組連接子句來執行左外接合
LEFT OUTER JOIN 包含聯結左側集合中的所有專案,以及聯結右側集合中的相符值。 在聯結中右側集合的任何項目,若在左側集合中沒有相符項目,將從查詢結果中排除。
子 Group Join
句實際上會執行 LEFT OUTER JOIN。 通常所知的 LEFT OUTER JOIN 與 Group Join
子句的差異在於:Group Join
子句會對左側集合中的每個項目,把來自右側集合的結果進行分組。 在關係資料庫中,LEFT OUTER JOIN 會傳回未分組的結果,其中查詢結果中的每個專案都包含聯結中兩個集合中的相符專案。 在此情況下,聯結左側集合中的項目會針對來自右側集合的每個相符項目重複。 當您完成下一個步驟時,您會看到此過程的結果。
您可以藉由擴充查詢來傳回每個群組查詢結果的項目,從而擷取查詢結果 Group Join
作為未分組的結果。 若要達成此目的,您必須確定您查詢 DefaultIfEmpty
群組集合的 方法。 這可確保聯結左側集合中的項目仍會包含在查詢結果中,即使它們沒有來自右側集合中的匹配結果。 您可以將程式代碼新增至查詢,以在聯結的右側集合中沒有相符的值時,提供預設結果值。
使用 Group Join 子句執行左外部聯接
將下列程式代碼新增至專案中的
Module1
模組,以查看群組左外部聯結和未分組的左外部聯結範例。Sub LeftOuterJoinExample() ' Create two lists. Dim people = GetPeople() Dim pets = GetPets(people) ' Grouped results. Dim petOwnersGrouped = From pers In people Group Join pet In pets On pers Equals pet.Owner Into PetList = Group Select pers.FirstName, pers.LastName, PetList ' Display grouped results. Dim output As New System.Text.StringBuilder For Each pers In petOwnersGrouped output.AppendFormat(pers.FirstName & ":" & vbCrLf) For Each pt In pers.PetList output.AppendFormat(vbTab & pt.Name & vbCrLf) Next Next Console.WriteLine(output) ' This code produces the following output: ' ' Magnus: ' Daisy ' Terry: ' Barley ' Boots ' Blue Moon ' Charlotte: ' Whiskers ' Arlene: ' "Flat" results. Dim petOwners = From pers In people Group Join pet In pets On pers Equals pet.Owner Into PetList = Group From pet In PetList.DefaultIfEmpty() Select pers.FirstName, pers.LastName, PetName = If(pet Is Nothing, String.Empty, pet.Name) ' Display "flat" results. output = New System.Text.StringBuilder() For Each pers In petOwners output.AppendFormat( pers.FirstName & ":" & vbTab & pers.PetName & vbCrLf) Next Console.WriteLine(output.ToString()) ' This code produces the following output: ' ' Magnus: Daisy ' Terry: Barley ' Terry: Boots ' Terry: Blue Moon ' Charlotte: Whiskers ' Arlene: End Sub
使用複合鍵進行聯結
您可以使用 And
關鍵詞在 Join
或 Group Join
子句中,識別在聯結集合的過程中用于匹配值的多個關鍵字段。 關鍵字 And
指定所有指定的鍵欄都必須匹配項目以進行聯結。
若要使用複合鍵執行連接
將下列程式代碼新增至專案中的
Module1
模組,以查看使用複合索引鍵的聯結範例。Sub CompositeKeyJoinExample() ' Create two lists. Dim people = GetPeople() Dim pets = GetPets(people) ' Implicit Join. Dim petOwners = From pers In people Join pet In pets On pet.Owner.FirstName Equals pers.FirstName And pet.Owner.LastName Equals pers.LastName Select pers.FirstName, PetName = pet.Name ' Display grouped results. Dim output As New System.Text.StringBuilder For Each pers In petOwners output.AppendFormat( pers.FirstName & ":" & vbTab & pers.PetName & vbCrLf) Next Console.WriteLine(output) ' This code produces the following output: ' ' Magnus: Daisy ' Terry: Barley ' Terry: Boots ' Terry: Blue Moon ' Charlotte: Whiskers End Sub
執行程式碼
若要新增程式代碼以執行範例
在專案中的
Sub Main
模組中,以以下列程式碼取代Module1
,以執行本主題中的範例。Sub Main() InnerJoinExample() LeftOuterJoinExample() CompositeKeyJoinExample() Console.ReadLine() End Sub
按 F5 執行範例。