Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to SELECT WHERE NOT EXIST using LINQ?

I have to list all "shift" data to be assigned to an "employee" but shift data must not be included if it is already existing in employee's data. Let's see the image sample.

No filtering yet

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

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

Let's see the result:

Filtered

Now my question is, how could I make this in linQ ? I'm using entity framework.
Hope someone could help. Thanks a lot!!!

like image 918
fiberOptics Avatar asked Jan 27 '12 09:01

fiberOptics


1 Answers

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

Hope this helps

like image 102
Arsen Mkrtchyan Avatar answered Sep 22 '22 19:09

Arsen Mkrtchyan