连接前,SQL会在新表中检索不同的记录

时间:2019-03-08 02:37:58

标签: sql join distinct

我有一个查询,我试图将Staff表连接到新表JobInfo

staff.StaffIDjobinfo.staff_id上。

var leaveSearchResult = 
(from staff in db.Staffs
 join leave in db.LeaveApplication
 on staff.StaffID equals leave.StaffID
 join appliedLeave in db.AppliedLeaves
 on leave.LeaveApplicationID equals appliedLeave.LeaveApplicationID
 join staffLeave in db.StaffLeaves
 on leave.StaffID equals staffLeave.StaffID
 join leaveType in db.LeaveTypes
 on staffLeave.LeaveID equals leaveType.LeaveTypeID
 where leave.LeaveStatus == LeaveBusinessLogic.AppliedLeaveStatus.Approved.ToString()

 //join JobInfo table based on staffid
 join jobinfo in db.JobInfo
 on staff.StaffID equals jobinfo.staff_id

 select new { staffDetails = staff, leaveDetails = leave, appliedLeaves = appliedLeave }).Distinct().AsQueryable();

JobInfo表中,每个staff_id的记录可能不止一个

如何基于staff_id表中的最高主键jobinfo_id为每个JobInfo检索1条记录?

例如

 join jobinfo in db.JobInfo

 on staff.StaffID equals (jobinfo.staff_id).OrderByDescending(jobinfo.jobinfo_id).Distinct()
 //^currently showing error

0 个答案:

没有答案