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

I try to execute the following code and get 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 get is: "The specified member of type Date is not supported in LINQ to Entities. Only initializers, entities, and entity navigation properties are supported." I tried various attempts to pour anythign into a string, comparing only the date, but it seems that it cannot get the correct combination. Any help is appreciated.

+46
c # linq linq-to-entities entity-framework
Aug 16 '11 at 19:29
source share
8 answers

Not the biggest solution, but it works. For a number of reasons, I have to use .net 3.5 at this point, and changing the database will be difficult. Anyway, 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.

+21
Aug 16 '11 at 20:05
source share

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 an earlier version of EF where DbFunctions not available, instead of EntityFunctions.TruncateTime(DateTime?) .

+80
Aug 01 2018-12-12T00:
source share
+3
Oct 26 '15 at 7:12
source share

Always use EntityFunctions.TruncateTime () for x.DateTimeStart and LogDate. eg:

 var query = from t in context.Logs where t.Title == title && EntityFunctions.TruncateTime(t.Timestamp) == EntityFunctions.TruncateTime(LogDate) select t; 
+2
Jun 13 '13 at 17:34
source share

Correct me if I am wrong, but in the mikemurf22 example, will he need to check every part of the date component and maybe a lot more server processing?

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

Assuming that you are only going to pass the date component, you can find the last minute of the day that you are going through and use the where clause to determine 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(); } 
+1
Oct 03 '11 at 13:41
source share

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; 
0
Jul 26 2018-12-12T00:
source share

Convert LongDate to .ToShortDateString , and then you can use it like this:

 EntityFunctions.TruncateTime(t.Timestamp) == LogDate 

as Mike did

0
Oct 30 '13 at 10:48 on
source share

Try the following:

 var calDate = DateTime.Now.Date.AddDays(-90); var result = return (from r in xyz where DbFunctions.TruncateTime(r.savedDate) >= DbFunctions.TruncateTime(calDate) 
0
Aug 20 '15 at 13:47
source share



All Articles