Method-Based Query Syntax Examples: Join Operators

 

The examples in this topic demonstrate how to use the Join and GroupJoin methods to query the AdventureWorks Sales Model using method-based query 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_MQ](../CodeSnippet/VS_Snippets_Data/DP L2E Examples#groupjoin2_mq)]

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_MQ](../CodeSnippet/VS_Snippets_Data/DP L2E Examples#groupjoin_mq)]

Example

The following example performs a join over the Contact and SalesOrderHeader tables.

[!CODE [DP L2E Examples#JoinSimple_MQ](../CodeSnippet/VS_Snippets_Data/DP L2E Examples#joinsimple_mq)]

Example

The following example performs a join over the Contact and SalesOrderHeader tables, grouping the results by contact ID.

[!CODE [DP L2E Examples#JoinWithGroupedResults_MQ](../CodeSnippet/VS_Snippets_Data/DP L2E Examples#joinwithgroupedresults_mq)]

Queries in LINQ to Entities

Show: