When using Linq2Sql to query the database on a specific datetime, the query always comes back with no results. I suspect the times are off by nanoseconds causing the difference. The times must be equal but not that equal. How do I query the database for a datetime, down to just the second for precision?
The following code works but it excludes the time portion altogether:
var Foos = Bars
.Where(x =>
DbFunctions.TruncateTime(x.EndDate) == DbFunctions.TruncateTime(endDate))
.Select(x => x.Id);
Also tried this code (which isn't pretty) but the compiler complained about having Date inside a linq query.
var Foo = Bars
.Where(x =>
x.EndDate.Value.Date == endDate.Value.Date &&
x.EndDate.Value.Hour == endDate.Value.Hour &&
x.EndDate.Value.Minute == endDate.Value.Minute &&
x.EndDate.Value.Second == endDate.Value.Second)
.Select(x => x.Id);
Also tried this, but the query always came back empty. Possibly ToString() not working in the Linq query.
var foo = bars.Where(x => x.EndDate.ToString() == endDate.ToString())