How can I use LINQ to SELECT WHERE NOT EXIST?

c# entity-framework linq sql

Question

All of the "shift" data must be included in order to be assigned to a "employee," however shift data that already exists in an employee's data must not be listed. View the picture sample now.

No filtering yet

This query solves the problem. I found this here:
Blog of Scott

select * from shift where not exists 
(select 1 from employeeshift where shift.shiftid = employeeshift.shiftid
and employeeshift.empid = 57);  

Let's examine the outcome:

Filtered

Now, how would I go about doing this in LinQ? I'm using entity framework.
I hope someone can assist. Many thanks!!

1
54
7/19/2013 11:52:23 PM

Accepted Answer

from s in context.shift
where !context.employeeshift.Any(es=>(es.shiftid==s.shiftid)&&(es.empid==57))
select s;

Hope this is useful.

94
1/27/2012 9:24:26 AM

Popular Answer

The sql output will change, but the outcome ought to be the same:

var shifts = Shifts.Where(s => !EmployeeShifts.Where(es => es.ShiftID == s.ShiftID).Any());


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow