Last active
June 2, 2016 16:53
-
-
Save aromig/d143aaa79d21d1010ae515c59425241d to your computer and use it in GitHub Desktop.
Entity LINQ to SQL Left Joins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example of multiple LEFT OUTER JOINs in LINQ query syntax | |
// Equivalent of: SELECT <blahblah> FROM requests r LEFT JOIN employees e ON e.emplid = r.emplid LEFT JOIN employees m ON m.emplid = r.mgrid LEFT JOIN locations l ON l.locid = request.locid | |
using System.Linq; | |
using (Entities db = new Entities()) | |
{ | |
var rows = | |
from request in db.requests | |
from employee in db.employees.Where(e => e.emplid == request.emplid).DefaultIfEmpty() | |
from manager in db.employees.Where(m => m.emplid == request.mgrid).DefaultIfEmpty() | |
from location in db.locations.Where(l => l.locid == request.locid).DefaultIfEmpty() | |
select new { Request = request, Employee = employee, Manager = manager, Location = location }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment