如何将sql查询转换为linq

时间:2016-08-22 10:49:51

标签: sql linq

我是linq的新手,如何将以下sql查询转换为linq

SELECT WorkingHoursID  
  FROM WorkingHours  
except
     ( 
       SELECT WorkingHoursID
         FROM Schedule
        WHERE Date = '2016-08-25'
     GROUP BY WorkingHoursID               
       HAVING COUNT( WorkingHoursID ) >= 8
     )

1 个答案:

答案 0 :(得分:2)

使用Except方法,您可以执行以下操作:

var schedules = (from s in DBCon.Schedule
                 where s.Date == new DateTime(2016, 8, 25)
                 group s by s.WorkingHoursID into g
                 where g.Count() >= 8
                 select g.Key).ToList();

var result = DBCon.WorkingHours.Select(item => item.WorkingHoursID).Except(schedules);