这是一个linq查询,以及表" Timesheet_Log"有DT_CR列的类型为DateTime,我想用最新的DT_CR日期获取查询,怎么可能?
var getEmployeeNames = (from emps in reslandentity.EMPLOYEE
join timesheet in reslandentity.TIMESHEET on emps.ID equals timesheet.RES_ID
join weekcal in reslandentity.WEEK_CALENDER on timesheet.WEEK_CAL_ID equals weekcal.ID
join log in reslandentity.TIMESHEET_LOG on timesheet.ID equals log.TIMESHEET_ID
join workflow in reslandentity.TIMESHEET_WORKFLOW on log.WORKFLOW_ID equals workflow.ID
where weekcal.WEEK_START_DT.Month == month
orderby log.DT_CR select new TimesheetModel
{
EMP_ID = emps.ID,
EMPLOYEE_NAME = emps.FIRST_NAME + " " + emps.LAST_NAME,
RES_TYPE = workflow.ORG_RES_TYPE,
EMP_STATUS = workflow.ACTION,
SDate = weekcal.WEEK_START_DT,
EDate = weekcal.WEEK_END_DT,
DT_CR=log.DT_CR
}).Distinct().ToList();
model.GetTimeSheetDetails = getEmployeeNames;
请帮我查询。
答案 0 :(得分:1)
如果你只想要一个结果或一个结果列表(每张时间表1个),你就不清楚了。
如果你只需要一个结果,那么lti Tyangi的答案就可以了。
如果您需要每个时间表的结果,您可以尝试以下方法:
var getEmployeeNames = (from emps in reslandentity.EMPLOYEE
join timesheet in reslandentity.TIMESHEET on emps.ID equals timesheet.RES_ID
join weekcal in reslandentity.WEEK_CALENDER on timesheet.WEEK_CAL_ID equals weekcal.ID
join mxLog in (
from lg in reslandentity.TIMESHEET_LOG
group lg by lg.TIMESHEET_ID into lgGrp
select new {lgGrp.Key, DT_CR = lgGrp.Max(x => x.DT_CR)}
) on timesheet.ID equals mxLog.Key
join log in reslandentity.TIMESHEET_LOG on new { a = mxLog.Key, b = mxLog.DT_CR} equals new{ a = log.TIMESHEET_ID, b = log.DT_CR}
join workflow in reslandentity.TIMESHEET_WORKFLOW on log.WORKFLOW_ID equals workflow.ID
where weekcal.WEEK_START_DT.Month == month
orderby log.DT_CR select new TimesheetModel
{
EMP_ID = emps.ID,
EMPLOYEE_NAME = emps.FIRST_NAME + " " + emps.LAST_NAME,
RES_TYPE = workflow.ORG_RES_TYPE,
EMP_STATUS = workflow.ACTION,
SDate = weekcal.WEEK_START_DT,
EDate = weekcal.WEEK_END_DT,
DT_CR=log.DT_CR
}).Distinct().ToList();
model.GetTimeSheetDetails = getEmployeeNames;
这里我们加入每个时间表的日志分组以获取每个时间表的最大日期,然后根据时间表的ID和该日期加入日志
答案 1 :(得分:0)
var getEmployeeNames = (from emps in reslandentity.EMPLOYEE
join timesheet in reslandentity.TIMESHEET on emps.ID equals timesheet.RES_ID
join weekcal in reslandentity.WEEK_CALENDER on timesheet.WEEK_CAL_ID equals weekcal.ID
join log in reslandentity.TIMESHEET_LOG on timesheet.ID equals log.TIMESHEET_ID
join workflow in reslandentity.TIMESHEET_WORKFLOW on log.WORKFLOW_ID equals workflow.ID
where weekcal.WEEK_START_DT.Month == month
//orderby log.DT_CR
select new TimesheetModel
{
EMP_ID = emps.ID,
EMPLOYEE_NAME = emps.FIRST_NAME + " " + emps.LAST_NAME,
RES_TYPE = workflow.ORG_RES_TYPE,
EMP_STATUS = workflow.ACTION,
SDate = weekcal.WEEK_START_DT,
EDate = weekcal.WEEK_END_DT,
DT_CR=log.DT_CR
}).OrderByDescending(x=>x.DT_CR).FirstorDefault();
OR
var getEmployeeNames = (from emps in reslandentity.EMPLOYEE
join timesheet in reslandentity.TIMESHEET on emps.ID equals timesheet.RES_ID
join weekcal in reslandentity.WEEK_CALENDER on timesheet.WEEK_CAL_ID equals weekcal.ID
join log in reslandentity.TIMESHEET_LOG on timesheet.ID equals log.TIMESHEET_ID
join workflow in reslandentity.TIMESHEET_WORKFLOW on log.WORKFLOW_ID equals workflow.ID
where weekcal.WEEK_START_DT.Month == month
//orderby log.DT_CR
select new TimesheetModel
{
EMP_ID = emps.ID,
EMPLOYEE_NAME = emps.FIRST_NAME + " " + emps.LAST_NAME,
RES_TYPE = workflow.ORG_RES_TYPE,
EMP_STATUS = workflow.ACTION,
SDate = weekcal.WEEK_START_DT,
EDate = weekcal.WEEK_END_DT,
DT_CR=log.DT_CR
}).OrderByDescending(x=>x.DT_CR).ToList().Take(1);