Query Expression Syntax Examples: Join Operators

 

Joining is an important operation in queries that target data sources that have no navigable relationships to each other, such as relational database tables. A join of two data sources is the association of objects in one data source with objects that share a common attribute in the other data source. For more information, see Standard Query Operators Overview.

The examples in this topic demonstrate how to use the GroupJoin and Join methods to query the AdventureWorks Sales Model using query expression syntax. The AdventureWorks Sales Model used in these examples is built from the Contact, Address, Product, SalesOrderHeader, and SalesOrderDetail tables in the AdventureWorks sample database.

The examples in this topic use the following using/Imports statements:

[!CODE [DP L2E Examples#ImportsUsing](../CodeSnippet/VS_Snippets_Data/DP L2E Examples#importsusing)]

Example

The following example performs a GroupJoin over the SalesOrderHeader and SalesOrderDetail tables to find the number of orders per customer. A group join is the equivalent of a left outer join, which returns each element of the first (left) data source, even if no correlated elements are in the other data source.

[!CODE [DP L2E Examples#GroupJoin2](../CodeSnippet/VS_Snippets_Data/DP L2E Examples#groupjoin2)]

Example

The following example performs a GroupJoin over the Contact and SalesOrderHeader tables to find the number of orders per contact. The order count and IDs for each contact are displayed.

[!CODE [DP L2E Examples#GroupJoin](../CodeSnippet/VS_Snippets_Data/DP L2E Examples#groupjoin)]

Example

The following example performs a GroupJoin over the Contact and SalesOrderHeader tables. A group join is the equivalent of a left outer join, which returns each element of the first (left) data source, even if no correlated elements are in the other data source.

[!CODE [DP L2E Examples#GroupJoin](../CodeSnippet/VS_Snippets_Data/DP L2E Examples#groupjoin)]

Example

The following example performs a join over the SalesOrderHeader and SalesOrderDetail tables to get online orders from the month of August.

[!CODE [DP L2E Examples#Join](../CodeSnippet/VS_Snippets_Data/DP L2E Examples#join)]

Queries in LINQ to Entities

Show: