c# - SQL server where clause equals instead of IN -
this question exact duplicate of:
- linq equal instead of contains 3 answers
here generated t-sql linq
select [extent1].[id] [id], [extent1].[name] [name] [dbo].[hospital] [extent1] ( exists (select 1 [c1] ( select [extent2].[id] [id] [dbo].[hospitaldepartment] [extent2] [extent1].[id] = [extent2].[hospitalid] ) [project1] exists (select 1 [c1] ( select [extent3].[id] [id] [dbo].[unit] [extent3] [project1].[id] = [extent3].[hospitaldepartmentid] ) [project2] exists (select 1 [c1] [dbo].[device] [extent4] ([project2].[id] = [extent4].[unitid]) , ([extent4].[devicetypeid] in (10,20)) ) )
i need use equal instead of in in following code because when used in returns result if contains 10 or 20 want need result if equals exactly 10 , 20
where exists (select 1 [c1] [dbo].[device] [extent4] ([project2].[id] = [extent4].[unitid]) , ([extent4].[devicetypeid] in (10,20)) )
and here linq source code:
var query = db.hospitals.asqueryable(); if (devicetypeids != null) { ienumerable<guid> selecteddevicetypeids = devicetypeids.split(',').select(guid.parse).asenumerable(); query = query.where(j => j.hospitaldepartments.any(jj => jj.units.any(m => m.devices.any(w => selecteddevicetypeids.contains(w.devicetypeid))))); } if (unittypeid != null) { query = query.where(j => j.hospitaldepartments.any(www => www.units.any(u => u.unittypeid == unittypeid))); } if (directorateofhealthid != null) { query = query.where(h => h.directoratehealthid == directorateofhealthid); } query = query.where(j => j.hospitaldepartments.any(u => u.units.any(d => d.devices.any(s => s.status == enums.devicestatus.free))) && j.hospitaldepartments.any(hd => hd.units.any(u => u.beds.any(b => b.status == enums.bedstatus.free)))); var list = query.tolist();
thank you
replace
m => m.devices.any(w => selecteddevicetypeids.contains(w.devicetypeid))
with
m => m.devices.all(w => selecteddevicetypeids.contains(w.devicetypeid)) && selecteddevicetypeids.all(w => m.devices.any(d => d.devicetypeid = w))
you can refactor this:
create method
private bool areequivalent(list<int> a, list<int> b) { return (a.count == b.count) && !a.except(b).any(); }
and
m => { var devicetypes = m.devices.select(d => d.devicetype).tolist(); return areequivalent(selecteddevicetypeids, devicetypes);}
Comments
Post a Comment