【发布时间】:2013-12-13 09:54:03
【问题描述】:
我有这个 SQL 查询需要转换为 LINQ。我是 LINQ 的新手,外连接使我更难将此查询转换为 LINQ。
select distinct ls.crew, sd.ambulance, case_number from log_sheet ls
left outer join shift_detail sd on ls.crew = sd.crew
and sd.time_on between dateadd(d,-1,'2013-05-30 21:48:04.000') and '2013-05-30 21:48:04.000' and (sd.time_off > '2013-05-30 21:48:04.000' or sd.time_off is null)
where ls.time_out between dateadd(d,-1,'2013-05-30 21:48:04.000') and '2013-05-30 21:48:04.000' and ('2013-05-30 21:48:04.000' <= ls.time_clear or ls.time_clear is null)
and (sd.ambulance = 58 or ls.crew = null)
union all
select distinct ls.crew, sd.ambulance, case_number from log_hist ls
left outer join shift_detail sd on ls.crew = sd.crew and sd.time_on between dateadd(d,-1,'2013-05-30 21:48:04.000') and '2013-05-30 21:48:04.000' and (sd.time_off > '2013-05-30 21:48:04.000' or sd.time_off is null)
where ls.time_out between dateadd(d,-1,'2013-05-30 21:48:04.000') and '2013-05-30 21:48:04.000' and ('2013-05-30 21:48:04.000' <= ls.time_clear or ls.time_clear is null)
and (sd.ambulance = 58 or ls.crew = null)
有人可以帮忙吗?我的失败尝试如下所示:
var shiftDetail = _dispatchRepository.FindQueryable<Domain.Model.ShiftDetail>();
var logsheet = _repository.FindQueryable<Domain.Model.LogSheet>()
.Where(ls => ((ls.time_out >= SqlFunctions.DateAdd("dd", -1, criteria.MRxIncident_Timestamp)) && (ls.time_out <= criteria.MRxIncident_Timestamp))
&& (criteria.MRxIncident_Timestamp <= ls.time_clear || ls.time_clear == null) && (ls.crew==null));
var shiftDetailQuery = from l in logsheet
join sd in shiftDetail on l.crew equals sd.crewID into LeftJoin
from sd in LeftJoin.DefaultIfEmpty()
select new MRxCaseSearchResults
{
CaseNumber = l.case_number,
Crew = l.crew,
Ambulance = sd.ambulance,
OfficerNo = sd.officer_no
};
【问题讨论】:
标签: c# sql linq outer-join