0

I'm trying to compare date values in entity framework.

DateTime selectedDate = Calendar1.SelectedDate;      
var result = context.EventsTable.Where(ev =>ev.EventDate.Equals(selectedDate));

ev.EventDate is coming from SQL Server 2008 and selectedDate is ASP:Calendar's Selected date. In SQL Server 2008 date is stored as: 2012-09-03 00:00:00 whereas date value from Calendar's SelectedDate is in 2012-09-03 12:00:00AM format.

hotcoder
  • 3,176
  • 10
  • 58
  • 96
  • Dates are dates. The display format has nothing to do with storage or computation. Is your code working? – SLaks Sep 12 '12 at 19:28
  • My code is working but comparison fails, although dates are same. – hotcoder Sep 12 '12 at 19:29
  • 1
    That probably means that they're different by a couple of milliseconds, or that there is a hidden time zone issue. – SLaks Sep 12 '12 at 19:30
  • 1
    Could be a rounding/precision problem if you use `datetime` as type in SQL Server: http://stackoverflow.com/a/11620980/270591 Note that `datetime` in SQL Server is less precise than `DateTime` in .NET. `datetime2` in SQL Server is better suited to be mapped from `DateTime` in .NET. – Slauma Sep 12 '12 at 19:43

1 Answers1

2

You should probably truncate EventDate:

context.EventsTable
    .Where(ev => EntityFunctions.TruncateTime(ev.EventDate) == selectedDate)

provided that selectedDate is truncated as well (by DateTime.Date).

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291