“The specified type member 'Date' is not supported in LINQ ”

后端 未结 4 921
野性不改
野性不改 2020-12-11 08:10
_dbEntities.EmployeeAttendances.Where(x => x.DailyDate.Date.Equals(DateTime.Now.Date)).ToList();

\"The specified type member \'Da

4条回答
  •  不知归路
    2020-12-11 08:54

    If the DailyDate property is already just a date, instead of a date and time, then it would be simplest to just use:

    // Outside the query so it becomes a constant, effectively
    var today = DateTime.Today;
    var employees = _dbEntities.EmployeeAttendances
                               .Where(x => x.DailyDate == today)
                               .ToList();
    

    If it does have times (making the above fail), you could always use:

    // Outside the query so it becomes a constant, effectively
    var today = DateTime.Today;
    var tomorrow = today.AddDays(1);
    var employees = _dbEntities.EmployeeAttendances
                               .Where(x => x.DailyDate >= today &&
                                           x.DailyDate < tomorrow)
                               .ToList();
    

    ... or use TruncateTime as Farhad's answer suggests. I'd still recommend evaluating DateTime.Today first though:

    var today = DateTime.Today;
    var employees = _dbEntities.EmployeeAttendances
                           .Where(x => EntityFunctions.TruncateTime(x.DailyDate) == today)
                           .ToList();
    

    Note that Today (like DateTime.Now) uses the system default time zone. You should think carefully about whether that's what you want.

提交回复
热议问题