如何编写SQL"不存在"在LINQ

时间:2014-06-12 04:55:02

标签: sql linq

这里是我的SQL查询

Select emp.ID,emp.First_Name,w.week_num,w.week_start_dt,w.id,w.week_end_dt 
from Employee emp,week_calender w
where not exists (select * from Timesheet where res_id=emp.id and week_cal_id=w.id)
and w.week_start_dt <= '08/06/2014'
order by w.ID

我写了像

这样的linq查询
var employeesNotEnteredList = (from emps in reslandentity.EMPLOYEE
                                           from week in reslandentity.WEEK_CALENDER
                                           where reslandentity.TIMESHEET.Any(m=>m.RES_ID!=emps.ID && m.WEEK_CAL_ID!=week.ID)
                                           && week.WEEK_START_DT.Month == month
                                           && week.WEEK_START_DT.Year == year
                                           && emps.COMP_ID == Companyid
                                           orderby week.ID
                                           select new TimesheetModel
                                           {
                                               EMP_ID = emps.ID,
                                               EMPLOYEE_NAME = emps.FIRST_NAME + " " + emps.LAST_NAME,
                                               RES_TYPE = "EMPLOYEE",
                                               SDate = week.WEEK_START_DT,
                                               EDate = week.WEEK_END_DT,
                                               EMP_STATUS = "NOT_ENTERED"
                                           }).Distinct().ToList();       
 model.GetTimesheetNotEnteredDetails = employeesNotEnteredList;

但是没有得到使用sql查询获得的确切结果

1 个答案:

答案 0 :(得分:2)

是的,明白了。 获得结果

 var employeesNotEnteredList = (from emps in reslandentity.EMPLOYEE
                                           from week in reslandentity.WEEK_CALENDER
                                           where !reslandentity.TIMESHEET.Any(m=>m.RES_ID==emps.ID && m.WEEK_CAL_ID==week.ID)
                                           && week.WEEK_START_DT.Month == month
                                           && week.WEEK_START_DT.Year == year
                                           && emps.COMP_ID == Companyid
                                           orderby week.ID
                                           select new TimesheetModel
                                           {
                                               EMP_ID = emps.ID,
                                               EMPLOYEE_NAME = emps.FIRST_NAME + " " + emps.LAST_NAME,
                                               RES_TYPE = "EMPLOYEE",
                                               SDate = week.WEEK_START_DT,
                                               EDate = week.WEEK_END_DT,
                                               EMP_STATUS = "NOT_ENTERED"
                                           }).Distinct().ToList();