How to SELECT WHERE NOT EXIST using LINQ?

前端 未结 5 808
余生分开走
余生分开走 2020-12-13 23:33

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 employ

5条回答
  •  甜味超标
    2020-12-13 23:59

    First of all, I suggest to modify a bit your sql query:

     select * from shift 
     where shift.shiftid not in (select employeeshift.shiftid from employeeshift 
                                 where employeeshift.empid = 57);
    

    This query provides same functionality. If you want to get the same result with LINQ, you can try this code:

    //Variable dc has DataContext type here
    //Here we get list of ShiftIDs from employeeshift table
    List empShiftIds = dc.employeeshift.Where(p => p.EmpID = 57).Select(s => s.ShiftID).ToList();
    
    //Here we get the list of our shifts
    List shifts = dc.shift.Where(p => !empShiftIds.Contains(p.ShiftId)).ToList();
    

提交回复
热议问题