'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported

mikemurf22 picture mikemurf22 · Aug 16, 2011 · Viewed 57.1k times · Source

I am trying to execute the following code and am receiving an error

public List<Log> GetLoggingData(DateTime LogDate, string title)
{
     var context = new LoggingEntities();
     var query = from t in context.Logs

           where t.Title == title 
           && t.Timestamp == LogDate

           select t;
     return query.ToList();
}

The error I'm receiving is "The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported." I have tried various attempts of casting everythign to a string, only comparing the date part, but can't seem to get the right combinaation. Any help is greatly appreciated.

Answer

Henrik Stenb&#230;k picture Henrik Stenbæk · Aug 1, 2012

If you are using EF 6.0+, you can use DbFunctions.TruncateTime(DateTime?) :

var query =
    from t in context.Logs
    where t.Title == title 
    && DbFunctions.TruncateTime(t.Timestamp) == LogDate.Date
    select t;

Note: For earlier version of EF where DbFunctions isn't available, EntityFunctions.TruncateTime(DateTime?) can be used instead.