Converting SQL to LINQ, Part 6: Joins (Bill Horst)

VBTeam

This post assumes you’ve read the previous posts in this series:

          Converting SQL to LINQ, Part 1: The Basics

          Converting SQL to LINQ, Part 2: FROM and SELECT

          Converting SQL to LINQ, Part 3: DISTINCT, WHERE, ORDER BY and Operators

          Converting SQL to LINQ, Part 4: Functions

          Converting SQL to LINQ, Part 5: GROUP BY and HAVING

This post will discuss Cross Join, Inner Join, Natural Join and Outer (Left/Right) Joins.

JOIN

It’s very common to query over more than one set of data (such as a table) in the same SQL SELECT statement.  Bringing together the information in multiple tables is called a join, and there are several kinds of joins in both SQL and LINQ.

Cross Join

The simplest join is a Cross Join, or Cartesian Join, which is a many-to-many join between two sets of data.  Each record in one set of data is joined to each record in another set.  In a SQL SELECT statement, this is done by specifying more than one table in a FROM clause.  In a VB LINQ expression, the same is true, as shown below.

SQL

SELECT CustomerTable.Name, OrderTable.OrderDate

FROM CustomerTable, OrderTable

 

VB

From Contact In CustomerTable, Shipment In OrderTable _

Select Contact.Name, Shipment.OrderDate

 

Inner Join

An Inner Join is a one-to-one join, where records in one set of data are matched up with records in another set of data based on certain common fields.  In a SQL SELECT statement, the second set of data is specified in an INNER JOIN clause, and the equalities used to join them are specified in an ON clause.  Similarly, the second set of data in a VB LINQ Join is specified in a Join clause, and an On clause is used to specify which field to match up with the Equals operator.

SQL

SELECT Contact.Name, Shipment.OrderID

FROM CustomerTable Contact

INNER JOIN OrderTable Shipment

ON Contact.CustomerID = Shipment.CustomerID

AND Contact.Zip = Shipment.ShippingZip

 

VB

From Contact In CustomerTable

Join Shipment In OrderTable _

On Contact.CustomerID Equals Shipment.CustomerID _

And Contact.Zip Equals Shipment.ShippingZip _

Select Contact.Name, Shipment.OrderID

 

The above example is an Equi-Join, meaning an equality operator is used to join information between the tables.  This is the only operator allowed in the On clause, so to emulate any other join operators (such as less-than), you will need to use a Cross Join filtered with a Where clause.

SQL

SELECT Contact.Name, Shipment.OrderID

FROM CustomerTable Contact

INNER JOIN OrderTable Shipment

ON Contact.CustomerID < Shipment.CustomerID

 

VB

From Contact In CustomerTable, Shipment In OrderTable _

Where Contact.CustomerID < Shipment.CustomerID

Select Contact.Name, Shipment.OrderID

 

Natural Join

A Natural Join is a one-to-one join where records in one set of data are matched up with records in another set of data based on all common fields (determined by matching names).  In a SQL SELECT statement, the second set of data can be specified in a NATURAL JOIN clause, and the equalities used to join the tables are implicit.  There is no direct equivalent to a natural join in VB LINQ expressions, so the best way to emulate it is to create an inner join and specify all common field equalities in the On clause manually.  This is more verbose than the SQL version, but should be pretty straightforward.

SQL

SELECT * FROM CustomerTable

NATURAL JOIN OrderTable

 

VB

From Contact In CustomerTable _

Join Shipment In OrderTable _

On Contact.CustomerID Equals Shipment.CustomerID _ And Contact.Phone Equals Shipment.Phone

 

Outer (Left/Right) Join

An Outer Join (also known as a Left Join or Right Join) is a one-to-many join, where each record in one set of data can be matched up with multiple records in another set of data, based on common fields.  In a SQL SELECT statement, the second set of data is specified in a LEFT JOIN or RIGHT JOIN clause, and the equalities used to join them are specified in an ON clause.  In a LEFT JOIN, every record in the first (left) set of data is joined with all records in the second set of data that match it based on the join expressions.  Every record in this first set of data will appear in the result, whether or not it matches anything in the second set.  This is reversed in a RIGHT JOIN, where everything in the second (right) set of data appears and is matched to everything possible in the first set.

The closest VB LINQ construct to an Outer Join would be a Group Join.  A Group Join clause specifies a second set of data, and provides the equality expressions in the On clause, much like a Join (Inner Join), described above.  There is also an Into clause, which can be used to specify aggregates to calculate over each group, much like in a Group Join clause above.

Similar to a SQL LEFT JOIN, each member of the first set of data is matched with everything that matches it in the second set of data.  Again, if there is no match for an item in the first set of data, it will still appear in the results.

 

0 comments

Leave a comment

Feedback usabilla icon