Skip to content

Instantly share code, notes, and snippets.

Last active June 2, 2016 16:53
What would you like to do?
Entity LINQ to SQL Left Joins
// 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