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:
using System; using System.Data; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Objects; using System.Globalization; using System.Data.EntityClient; using System.Data.SqlClient; using System.Data.Common;
GroupJoin
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.
using (AdventureWorksEntities context = new AdventureWorksEntities()) { ObjectSet<SalesOrderHeader> orders = context.SalesOrderHeaders; ObjectSet<SalesOrderDetail> details = context.SalesOrderDetails; var query = from order in orders join detail in details on order.SalesOrderID equals detail.SalesOrderID into orderGroup select new { CustomerID = order.SalesOrderID, OrderCount = orderGroup.Count() }; foreach (var order in query) { Console.WriteLine("CustomerID: {0} Orders Count: {1}", order.CustomerID, order.OrderCount); } }
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.
using (AdventureWorksEntities context = new AdventureWorksEntities()) { ObjectSet<Contact> contacts = context.Contacts; ObjectSet<SalesOrderHeader> orders = context.SalesOrderHeaders; var query = from contact in contacts join order in orders on contact.ContactID equals order.Contact.ContactID into contactGroup select new { ContactID = contact.ContactID, OrderCount = contactGroup.Count(), Orders = contactGroup }; foreach (var group in query) { Console.WriteLine("ContactID: {0}", group.ContactID); Console.WriteLine("Order count: {0}", group.OrderCount); foreach (var orderInfo in group.Orders) { Console.WriteLine(" Sale ID: {0}", orderInfo.SalesOrderID); } Console.WriteLine(""); } }
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.
using (AdventureWorksEntities context = new AdventureWorksEntities()) { ObjectSet<Contact> contacts = context.Contacts; ObjectSet<SalesOrderHeader> orders = context.SalesOrderHeaders; var query = from contact in contacts join order in orders on contact.ContactID equals order.Contact.ContactID into contactGroup select new { ContactID = contact.ContactID, OrderCount = contactGroup.Count(), Orders = contactGroup }; foreach (var group in query) { Console.WriteLine("ContactID: {0}", group.ContactID); Console.WriteLine("Order count: {0}", group.OrderCount); foreach (var orderInfo in group.Orders) { Console.WriteLine(" Sale ID: {0}", orderInfo.SalesOrderID); } Console.WriteLine(""); } }
Join
Example
The following example performs a join over the SalesOrderHeader and SalesOrderDetail tables to get online orders from the month of August.
using (AdventureWorksEntities context = new AdventureWorksEntities()) { ObjectSet<SalesOrderHeader> orders = context.SalesOrderHeaders; ObjectSet<SalesOrderDetail> details = context.SalesOrderDetails; var query = from order in orders join detail in details on order.SalesOrderID equals detail.SalesOrderID where order.OnlineOrderFlag == true && order.OrderDate.Month == 8 select new { SalesOrderID = order.SalesOrderID, SalesOrderDetailID = detail.SalesOrderDetailID, OrderDate = order.OrderDate, ProductID = detail.ProductID }; foreach (var order in query) { Console.WriteLine("{0}\t{1}\t{2:d}\t{3}", order.SalesOrderID, order.SalesOrderDetailID, order.OrderDate, order.ProductID); } }
See Also
Build Date: