Click here to Skip to main content
15,887,464 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have some errors that occur with this search function. I have two tables: employees and attendance. In table, attendance has AddendanceDate(datetime) column. I want to get the results absent lists of employee for today. For datetime.today, the result is good, but I want to choice the search keyword with other dates. How can I assign in this following code. When I assign a.AttendanceDate== date, I don't get the result.

My code is as follows:
C#
public async Task<IEnumerable<AbsentReportsViewModel>> GetAllAbsentEmpAsync(DateTime date)
{
     var query = await (from e in _dbContext.Employees
                       join attt in _dbContext.Attendances
                       on e.Id equals attt.EmployeeId into eGroup
                       from eattt in eGroup.Where
                       (a => a.AttendanceDate ==
                                DateTime.Today ).DefaultIfEmpty()
                       where eattt.AttendanceDate == null
                       select new AbsentReportsViewModel
                       {
                           JobId = e.JobId,
                           EmpReportName = e.NameMM,
                           DeptId = e.DeptId,
                           RankId = e.CurrentRankId,
                           DeptReportName = e.Department.DeptName,
                           RankReportName = e.Rank.RankName,
                           BarCode = e.Barcode,
                           EmpReportId = e.Id,
                       }).ToListAsync();

    return query;
}


What I have tried:

C#
public async Task<IEnumerable<AbsentReportsViewModel
>> GetAllAbsentEmpAsync(DateTime date) 
{
    var query = await (from e in _dbContext.Employees
                       join attt in _dbContext.Attendances
                       on e.Id equals attt.EmployeeId into eGroup
                       from eattt in eGroup.Where(a => 
                       a.AttendanceDate == date ).DefaultIfEmpty()
                       where eattt.AttendanceDate == null
                       select new AbsentReportsViewModel
                       {   
                           JobId = e.JobId,
                           EmpReportName = e.NameMM,
                           DeptId = e.DeptId,
                           RankId = e.CurrentRankId,
                           DeptReportName = e.Department.DeptName,
                           RankReportName = e.Rank.RankName,
                           BarCode = e.Barcode,
                           EmpReportId = e.Id,
                       }).ToListAsync();           
    return query;
}
Posted
Updated 13-Sep-23 6:40am
v2
Comments
George Swan 30-Aug-23 1:38am    
Have you tried comparing dates rather than date and time. Something like this a.AttendanceDate.Date == date.Date ?

1 solution

Start with the debugger: look at exactly what date contains.
Unless it specifically has midnight as the time and your DB AttendanceDate also has midnight a DateTime comparison will not work unless you are extremely lucky.

Since DateTime.Today matches OK, the most likely reason is that the date passed in includes a time component.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900