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
必須。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. AJoin句は、別の入れ子にすることができます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 key2key1 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.

結合条件で使用されるキーは、コレクションの 1 つ以上の項目を含む式を指定できます。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. これに相当するINNER JOINsql です。This is equivalent to an INNER JOIN in SQL.

複数を使用するJoin1 つのコレクションに 2 つ以上のコレクションを結合するクエリ内の句。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. これを行うには、複数In内の句、From句を指定し、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. これは似ています、 LEFT OUTER JOIN sql です。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

次のコード例では、2 つのコレクションを結合を使用して、Join句。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

関連項目See also