Join 句 (Visual Basic)Join Clause (Visual Basic)
2 つのコレクションを単一のコレクションに結合します。Combines two collections into a single collection. 結合操作は、一致するキーに基づき、Equals
演算子を使用します。The join operation is based on matching keys and uses the Equals
operator.
構文Syntax
Join element In collection _
[ joinClause _ ]
[ groupJoinClause ... _ ]
On key1 Equals key2 [ And key3 Equals key4 [... ]
指定項目Parts
element
必須。element
Required. 結合されるコレクションの制御変数。The control variable for the collection being joined.
collection
必須です。Required. Join
演算子の左側に指定されているコレクションと結合するコレクション。The collection to combine with the collection identified on the left side of the Join
operator. Join
句は、別の Join
句、または Group Join
句で入れ子にすることができます。A Join
clause can be nested in another Join
clause, or in a Group Join
clause.
joinClause
任意。Optional. クエリをさらに絞り込むための 1 つ以上の追加の Join
句。One or more additional Join
clauses to further refine the query.
groupJoinClause
任意。Optional. クエリをさらに絞り込むための 1 つ以上の追加の Group Join
句。One or more additional Group Join
clauses to further refine the query.
key1
Equals
key2
key1
Equals
key2
必須です。Required. 結合されるコレクションのキーを識別します。Identifies keys for the collections being joined. Equals
演算子を使用して、結合されるコレクションのキーを比較する必要があります。You must use the Equals
operator to compare keys from the collections being joined. 結合条件を組み合わせるには、複数のキーを識別するために、And
演算子を使用します。You can combine join conditions by using the And
operator to identify multiple keys. key1
は、Join
演算子の左側にあるコレクションのものである必要があります。key1
must be from the collection on the left side of the Join
operator. key2
は、Join
演算子の右側にあるコレクションのものである必要があります。key2
must be from the collection on the right side of the Join
operator.
結合条件で使用されるキーは、コレクションからの複数の項目を含む式にすることができます。The keys used in the join condition can be expressions that include more than one item from the collection. ただし、各キー式には、それぞれのコレクションの項目のみを含めることができます。However, each key expression can contain only items from its respective collection.
RemarksRemarks
Join
句は、結合されるコレクションからの一致するキー値に基づいて、2 つのコレクションを組み合わせます。The Join
clause combines two collections based on matching key values from the collections being joined. 結果のコレクションには、Join
演算子の左側に指定されたコレクションと、Join
句で指定されたコレクションからの値の任意の組み合わせが含まれる可能性があります。The resulting collection can contain any combination of values from the collection identified on the left side of the Join
operator and the collection identified in the Join
clause. クエリから返されるのは、Equals
演算子によって指定された条件を満たした結果だけです。The query will return only results for which the condition specified by the Equals
operator is met. これは、SQL の INNER JOIN
と同じです。This is equivalent to an INNER JOIN
in SQL.
クエリで複数の Join
句を使用して、複数のコレクションを 1 つのコレクションに結合できます。You can use multiple Join
clauses in a query to join two or more collections into a single collection.
暗黙的結合を実行して、Join
句を使用せずにコレクションを結合することができます。You can perform an implicit join to combine collections without the Join
clause. これを行うには、From
句に複数の In
句を含め、結合に使用するキーを識別する Where
句を指定します。To do this, include multiple In
clauses in your From
clause and specify a Where
clause that identifies the keys that you want to use for the join.
Group Join
句を使用して、コレクションを、単一の階層コレクションに結合することができます。You can use the Group Join
clause to combine collections into a single hierarchical collection. これは、SQL の LEFT OUTER JOIN
に似ています。This is like a LEFT OUTER JOIN
in SQL.
例Example
次のコード例では、暗黙的結合を実行して、顧客のリストと注文を結合しています。The following code example performs an implicit join to combine a list of customers with their orders.
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
例Example
次のコード例では、Join
句を使用して 2 つのコレクションを結合しています。The following code example joins two collections by using the Join
clause.
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
この例では、次のような出力が生成されます。This example will produce output similar to the following:
winlogon (968), Windows Logon
explorer (2424), File Explorer
cmd (5136), Command Window
例Example
次のコード例では、2 つのキー列で Join
句を使用して、2 つのコレクションを結合しています。The following code example joins two collections by using the Join
clause with two key columns.
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
この例では、次のような出力が生成されます。The example will produce output similar to the following:
winlogon (968), Windows Logon, Priority = 13
cmd (700), Command Window, Priority = 8
explorer (2424), File Explorer, Priority = 8