我必须列出要分配给“员工”的所有“班次”数据,但如果员工数据中已存在班次数据,则不得包含班次数据。我们来看看图片样本。
此查询解决了该问题。我在这里找到了这个:
Scott's Blog
select * from shift where not exists
(select 1 from employeeshift where shift.shiftid = employeeshift.shiftid
and employeeshift.empid = 57);
让我们看看结果:
现在我的问题是,我怎么能在linQ中做到这一点?我正在使用实体框架 希望有人能提供帮助。非常感谢!!!
答案 0 :(得分:83)
from s in context.shift
where !context.employeeshift.Any(es=>(es.shiftid==s.shiftid)&&(es.empid==57))
select s;
希望这有帮助
答案 1 :(得分:23)
结果sql会有所不同,但结果应该是相同的:
var shifts = Shifts.Where(s => !EmployeeShifts.Where(es => es.ShiftID == s.ShiftID).Any());
答案 2 :(得分:2)
首先,我建议修改一下你的sql查询:
select * from shift
where shift.shiftid not in (select employeeshift.shiftid from employeeshift
where employeeshift.empid = 57);
此查询提供相同的功能。 如果您想通过LINQ获得相同的结果,可以尝试以下代码:
//Variable dc has DataContext type here
//Here we get list of ShiftIDs from employeeshift table
List<int> empShiftIds = dc.employeeshift.Where(p => p.EmpID = 57).Select(s => s.ShiftID).ToList();
//Here we get the list of our shifts
List<shift> shifts = dc.shift.Where(p => !empShiftIds.Contains(p.ShiftId)).ToList();
答案 3 :(得分:-1)
怎么样..
var result = (from s in context.Shift join es in employeeshift on s.shiftid equals es.shiftid where es.empid == 57 select s)
编辑:这将为您提供相关员工转移(因为加入)的转变。对于“不存在”,我会做@ArsenMkrt或@hyp建议
答案 4 :(得分:-1)
Dim result2 = From s In mySession.Query(Of CSucursal)()
Where (From c In mySession.Query(Of CCiudad)()
From cs In mySession.Query(Of CCiudadSucursal)()
Where cs.id_ciudad Is c
Where cs.id_sucursal Is s
Where c.id = IdCiudad
Where s.accion <> "E" AndAlso s.accion <> Nothing
Where cs.accion <> "E" AndAlso cs.accion <> Nothing
Select c.descripcion).Single() Is Nothing
Where s.accion <> "E" AndAlso s.accion <> Nothing
Select s.id, s.Descripcion