我对Sql和Linq不太熟练。我有五张桌子
LookupType
LookupTypeId LookupTypeName 3 Days
LookupValue
LookupValueId LookupTypeId Value
1 3 Monday
2 3 Tuesday
... .. ...
... ... ...
7 3 Sunday
工作
JobId
6
JobRequest
JobRequestId DayId JobId
1 2 6
2 4 6
DayId来自LookupValue表(LookupValueId)
JobRequestShift
JobRequestShiftId JobRequestId Time
1 1 03:10:56
2 1 05:10:56
3 2 03:11:03
4 2 04:26:14
5 2 05:10:56
Job与JobRequest有一对多的关系,JobRequest与JobRequestShift有一对多的关系。 我想要格式为
的数据DayName JobRequestId JobRequestShifts(List of JobRequestShifts)
Monday null null
Tuesday 1 JobRequestShiftId JobRequestId Time
1 1 03:10:56
2 1 05:10:56
Wednesday null null
Thursday 2 JobRequestShiftId JobRequestId Time
3 2 03:11:03
4 2 04:26:14
5 2 05:10:56
Friday null null
Saturday null null
Sunday null null
In an attempt to get the desired result I have come up with this
from lt in LookupTypes
join lv in LookupValues
on lt.LookupTypeId equals lv.LookupTypeId
join jr in JobRequest.Where(rcr => rcr.JobId == jobId)
//join jr in JobRequest.Include(jr => jr.JobRequestShifts).Where(rcr => rcr.JobId == jobId)
//got an exception
on lv.LookupValueId equals jr.DayId into SelectedDays
from sd in SelectedDays.DefaultIfEmpty()
join jrs in JobRequestShifts
on sd.JobRequestId equals jrs.JobRequestId into JobShifts
from rs in JobShifts.DefaultIfEmpty()
where lt.LookupTypeName == "Days"
select new
{
lv,
sd,
Shifts = JobShifts
}
之后我打算在我的Entity类上投射结果。