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

C#Entity FrameworkLinqLinq to-Entities

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

Not the greatest solution, but it works. For a variety of reasons, I have to use .net 3.5 at this point and modifying the database would be difficult. Anyways, here is a solution that works:

            var query = from t in context.Logs
                      where t.Title == title 
                      && t.Timestamp.Day == LogDate.Day
                      && t.Timestamp.Month == LogDate.Month
                      && t.Timestamp.Year == LogDate.Year
                      select t;

Not the most elegant solution, but it is effective.

Solution 3 - C#

Solution 4 - C#

Always use EntityFunctions.TruncateTime() for both x.DateTimeStart and LogDate. such as :

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

Solution 5 - C#

Correct me if I'm wrong, but in mikemurf22's example, it would need to check each part of the date component, and potentially a lot more server processing?

Anyway, I stumbled across this problem, and this is my solution.

Assuming that you're going to be passing in the date component only, you can find the last minute of the day that you pass in, and use the where clause to define the range.

public List<Log> GetLoggingData(DateTime LogDate, string title)
{
    DateTime enddate = new DateTime(LogDate.Year, LogDate.Month, LogDate.Day, 23, 59, 59)

    var query = from t in context.Logs
                where t.Timestamp >= date
                where t.Timestamp <= enddate
                select t;

    return query.ToList();
}

Solution 6 - C#

Convert LongDate to .ToShortDateStringand then you can use it this way:

EntityFunctions.TruncateTime(t.Timestamp) == LogDate

like mike did

Solution 7 - C#

Try this:

var calDate = DateTime.Now.Date.AddDays(-90);

var result = return (from r in xyz where DbFunctions.TruncateTime(r.savedDate) >= DbFunctions.TruncateTime(calDate)

Solution 8 - C#

You can use this hack:

DateTime startDate = LogDate.Date;
DateTime endDate = LogDate.Date.AddDays(1);

var query = from t in context.Logs
            where t.Title == title 
                  && t.Timestamp >= startDate 
                  && t.Timestamp < endDate
		    select t;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmikemurf22View Question on Stackoverflow
Solution 1 - C#Henrik StenbækView Answer on Stackoverflow
Solution 2 - C#mikemurf22View Answer on Stackoverflow
Solution 3 - C#Rahul NikateView Answer on Stackoverflow
Solution 4 - C#Babul MirdhaView Answer on Stackoverflow
Solution 5 - C#MikeView Answer on Stackoverflow
Solution 6 - C#sansalkView Answer on Stackoverflow
Solution 7 - C#user3783446View Answer on Stackoverflow
Solution 8 - C#algreatView Answer on Stackoverflow