Search code examples
entity-frameworklinqentity-framework-6entity-framework-4

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();

When I try to run the above statement, I get the following error:

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

I thought that DbFunctions.TruncateTime() method would solve this problem as suggested in many posts I have seen, however I still get the error. I also tried to just apply the method to the database value but I still get the same error.

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

Solution

  • You need to pull DateTime into variable before LINQ:

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