Topic: Doing Left/Right Joins |
Posted On: 9/3/2008 8:26 PM
Posted By: Ish Singh
|
One of the most frequent questions we get in email is how to do a left join. Here is a short code snippet that does just that.
| using (Orders o = new Orders()) |
| { |
| o.Join_SalesPersonID_Users_Parent(JoinTypes.Left); |
| o.Find(); |
| } |
Now if you using the strongly typed collection and you need to check if there is no associated User record you can do it like this:
| foreach (Orders order in o.List) |
| { |
| if (!order.SalesPersonID.IsNull) |
| { |
| MessageBox.Show(order.SalesPersonID_Users_Parent.FirstName.Value |
| + " " + |
| order.SalesPersonID_Users_Parent.LastName.Value |
| ); |
| } |
| } |
| |
If you want to create LEFT JOIN and still use LINQ thats very easy as well. Following is a quick example that shows how to do just that.
| using (Orders orders = new Orders()) | | { | | orders.Join_SalesPersonID_Users_Parent(JoinTypes.Left); | | | | // Return selective fields | | var result = ( | | from o in orders | | select new { o.OrderAmount, o.OrderDate, o.OrderID, | | SoldBy = o.SalesPersonID_Users_Parent.FirstName + " " + o.SalesPersonID_Users_Parent.LastName } | | ).ToList(); | | this.GridView1.DataSource = result; | | } | | |
| 9/3/2008 9:34:32 PM Ish Singh
|