问题
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.
回答1:
You should probably truncate EventDate
:
context.EventsTable
.Where(ev => EntityFunctions.TruncateTime(ev.EventDate) == selectedDate)
provided that selectedDate
is truncated as well (by DateTime.Date
).
来源:https://stackoverflow.com/questions/12394954/compare-date-values-in-entity-framework