You can use EF to create basically the same queries posted in the question. I started by creating a poco model EmployeePrivilege with properties: int PrivilegeID & int EmployeeID. I did not add this to the DbContext.
var EmpPrivQuery = ctx.Privileges
.Where(p => p.PrivilegeName == "P3")
.SelectMany(p => p.Employees, (p, e) => new EmployeePrivilege{EmployeeID = e.EmployeeID, PrivilegeID = p.PrivilegeID}
.Distinct();
var employeesMissingPrivilege = from e in Employees
join epq in EmpPrivQuery
on e.EmployeeID equals epq.EmployeeID
into jointable
where jointable.Count()==0
select e;
I just realized you can also get the same result without creating the poco EmployeePrivilege as follows:
var EmpPrivQuery = ctx.Privileges
.Where(p => p.PrivilegeName == "P3")
.SelectMany(p => p.Employees.Select(e => e.EmployeeID)
.Distinct();
var employeesMissingPrivilege = from e in Employees
join epq in EmpPrivQuery
on e.EmployeeID equals epq
into jointable
where jointable.Count()==0
select e;
Both of these EF queries return Employees missing specified privileges against both Sql Server and Oracle (using Devart's dotConnect for Oracle).
Many posts that I read referred to using DefaultIfEmpty()
to achieve a left outer join. The queries above work, however, please post if there is a better way to this result using DefaultIfEmpty()
.