Group Join Clause (Visual Basic)
Combines two collections into a single hierarchical collection. The join operation is based on matching keys.
Group Join element [As type] In collection _ On key1 Equals key2 [ And key3 Equals key4 [... ] ] _ Into expressionList
The Group Join clause combines two collections based on matching key values from the collections being joined. The resulting collection can contain a member that references a collection of elements from the second collection that match the key value from the first collection. You can also specify aggregate functions to apply to the grouped elements from the second collection. For information about aggregate functions, see Aggregate Clause (Visual Basic).
Consider, for example, a collection of managers and a collection of employees. Elements from both collections have a ManagerID property that identifies the employees that report to a particular manager. The results from a join operation would contain a result for each manager and employee with a matching ManagerID value. The results from a Group Join operation would contain the complete list of managers. Each manager result would have a member that referenced the list of employees that were a match for the specific manager.
The collection resulting from a Group Join operation can contain any combination of values from the collection identified in the From clause and the expressions identified in the Into clause of the Group Join clause. For more information about valid expressions for the Into clause, see Aggregate Clause (Visual Basic).
A Group Join operation will return all results from the collection identified on the left side of the Group Join operator. This is true even if there are no matches in the collection being joined. This is like a LEFT OUTER JOIN in SQL.
You can use the Join clause to combine collections into a single collection. This is equivalent to an INNER JOIN in SQL.
The following code example joins two collections by using the Group Join clause.
Dim customerList = From cust In customers _ Group Join ord In orders On _ cust.CustomerID Equals ord.CustomerID _ Into CustomerOrders = Group, _ OrderTotal = Sum(ord.Total) _ Select cust.CompanyName, cust.CustomerID, _ CustomerOrders, OrderTotal For Each customer In customerList Console.WriteLine(customer.CompanyName & _ " (" & customer.OrderTotal & ")") For Each order In customer.CustomerOrders Console.WriteLine(vbTab & order.OrderID & ": " & order.Total) Next Next