2013-09-05 189 views
3

我有兩個表「員工」和「EmployeesCompanies」,員工包含了所有員工的列表,並employeescompanies包含與員工相關的所有公司的名單:LINQ到SQL左外連接不

 
Table 1 (Employees) 
EmployeeID 
1 
2 
3 

Table 2 (EmployeesCompanies) 
EmployeeID  
1 
2 

我想回到 3是從EmployeesCompanies缺少的記錄,這裏是我使用的LINQ代碼:

var queryOrphanedEmployees = (from a in db.Employees 
join b in db.EmployeesCompanies 
on a.EmployeeID equals b.EmployeeID 
into outer 
from c in outer.DefaultIfEmpty() 
select new { a.EmployeeID}).ToList(); 

然而,這將返回:

 
1 
2 

這與我想要的完全相反。

回答

2

你應該能夠做這樣的事,如果你已經設置了外鍵正確

from e in Employees 
where !e.EmployeesCompanies.Any() 
select e