简体   繁体   中英

Linq to SQL Left Outer Join Not

I have two Tables "Employees" and "EmployeesCompanies", Employees contains a list of all employees, and employeescompanies contains a list of all companies associated with an employee:

Table 1 (Employees)
EmployeeID
1
2
3

Table 2 (EmployeesCompanies)
EmployeeID    
1
2

I want to return 3 which is the missing record from EmployeesCompanies, here is the linq code I'm using:

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();

However this returns:

1
2

Which is exactly opposite to what I want.

You should be able to do something like this if you've set up the foreign keys properly

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM