LINQ to Entities 不支持指定的类型成员 'Date'。 DbFunctions.TruncateTime()

The specified type member 'Date' is not supported in LINQ to Entities. DbFunctions.TruncateTime()

Question question = 
         db.Questions
           .Where(q => DbFunctions.TruncateTime(q.Day.DayDate) == DbFunctions.TruncateTime(DateTime.Now.Date))
           .Where(q => q.Order == id)
           .FirstOrDefault();

当我尝试 运行 上述语句时,出现以下错误:

The specified type member 'Date' is not supported in LINQ to Entities

我认为 DbFunctions.TruncateTime() 方法可以解决这个问题,正如我看到的许多帖子中所建议的那样,但是我仍然遇到错误。我也尝试将该方法应用于数据库值,但我仍然遇到相同的错误。

Question question = 
         db.Questions
           .Where(q => DbFunctions.TruncateTime(q.Day.DayDate) == DateTime.Now.Date)
           .Where(q => q.Order == id) 
           .FirstOrDefault();

您需要在 LINQ 之前将 DateTime 拉入变量:

var dateOfNow = DateTime.Now.Date;

Question question = 
         db.Questions
           .Where(q => DbFunctions.TruncateTime(q.Day.DayDate) == dateOfNow)
           .Where(q => q.Order == id) 
           .FirstOrDefault();