50

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.

Rahul Nikate
  • 6,192
  • 5
  • 42
  • 54
mikemurf22
  • 1,851
  • 2
  • 21
  • 32
  • 1
    Show us the Logs class and the table it's being mapped to. – BennyM Aug 16 '11 at 19:35
  • I am using EFv1 with .net 3.5. The log class is just a log table from the enterprise library with the title as a String type and Timestamp as a datetime field. I just want to compare the datepart – mikemurf22 Aug 16 '11 at 19:41

8 Answers8

90

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.

Xiaoy312
  • 14,292
  • 1
  • 32
  • 44
Henrik Stenbæk
  • 3,982
  • 5
  • 31
  • 33
  • You will also have to Truncate your LogDate.Date property – invalidusername Dec 31 '12 at 20:47
  • 1
    @invalidusername LogDate is a DateTime which means that LogDate.Date is "truncated" – Henrik Stenbæk Jan 09 '13 at 05:26
  • 3
    +1 for you sir. I couldn't use the accepted answer in my case as I did a 'greater than' comparison, in which you can't compare on Day, Month and Year separately. – HTBR Feb 26 '13 at 15:33
  • 2
    Didn't know about the functions. Fantastic. – Lotok Jun 07 '13 at 09:51
  • 19
    BTW -- That function is now obsolete and has been moved to another assembly. Use 'DbFunctions.TruncateTime(t.Timestamp)' instead. – Bob Jan 23 '14 at 15:57
  • Make sure to put your date in a variable. I did a compare like 'DbFunctions.TruncateTime(t.Timestamp) == DateTime.Now.Date', but that doesn't work. 'var today = DateTime.Now.Date; DbFunctions.TruncateTime(t.Timestamp) == today works! – kloarubeek Aug 11 '19 at 21:18
25

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.

mikemurf22
  • 1,851
  • 2
  • 21
  • 32
  • 1
    The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported. – SAR May 07 '17 at 08:30
3

EntityFunctions.TruncateTime(t.Timestamp) is obsolete from EF6.

Use below

DbFunctions.TruncateTime(t.Timestamp)

Rahul Nikate
  • 6,192
  • 5
  • 42
  • 54
2

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;
Babul Mirdha
  • 3,816
  • 1
  • 22
  • 25
1

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();
}
John Bledsoe
  • 17,142
  • 5
  • 42
  • 59
Mike
  • 1,532
  • 3
  • 21
  • 45
0

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

EntityFunctions.TruncateTime(t.Timestamp) == LogDate

like mike did

fbarikzehy
  • 4,885
  • 2
  • 33
  • 39
sansalk
  • 4,595
  • 2
  • 36
  • 37
0

Try this:

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

var result = return (from r in xyz where DbFunctions.TruncateTime(r.savedDate) >= DbFunctions.TruncateTime(calDate)
Jemar Jones
  • 1,491
  • 2
  • 21
  • 27
user3783446
  • 425
  • 5
  • 11
-1

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;
algreat
  • 8,592
  • 5
  • 41
  • 54
  • I corrected it a little. Of cource you can't use AddDays method inside your query. But you can move it outside. So workaround still works. – algreat Mar 26 '14 at 14:35