Let's suppose I have two tables:
- Employees table: contains fields such as EmployeeId, DepartmentId...
- Signings/Attendance table: contains fields such as SigningId, Time, EmployeeId, SignType (in or out)...
I am trying to select the last signing from every employee who belongs to a certain department in a certain DateTime (usually DateTime.Now) because I am developing an attendance-based application for my company.
So far I have the following query, which returns EmployeeId and SignType for all employees who meet the previous conditions (department and time). I know this query is supposed to have an extra join (left join to be exact probably thanks to this post I found yesterday) but do not really know how to proceed since it is my first time doing such complex linq queries.
Thanks a lot in advance.
from e in AppDbContext.Employees
join s in AppDbContext.Signings
on e.EmployeeId equals s.EmployeeId
where e.DepartmentId == DepartmentId
&& s.Time.Date == DateTime.Now.Date
select new Aux2()
{
Id = e.EmployeeId,
Type = s.SignType,
};
EDIT: subquery attempt
AppDbContext.Employees
.Where(e => e.DepartmentId == DepartmentId )
.Select(e => new Aux2(){
Id = e.EmployeeId,
Type = AppDbContext.Signings
.Where(s => s.EmployeeId== e.EmployeeId && s.Time.Date==Date)
.OrderByDescending(s => s.Time)
.Take(1)
.Select(s => s.SigType)
.FirstOrDefault()
})