I’m trying to check if a DateTime is already in my db.
I’ve broken up the code below in order to get a better understanding of what’s going on but despite my best efforts, dateChecks will always return an empty list.
var MyDate = DateTime.Now;
foreach (var milestone in mtMilestone.Milestones)
{
var checkIfAlreadyPulled = _ipdb.JsonDataReleases.
Where(x => x.ReleaseId == mtMilestone.ReleaseId).ToList();
var dateChecks = checkIfAlreadyPulled.
Where(x => EntityFunctions.Equals(x.LastUpdated, MyDate)).ToList();
if (!dateChecks.Any())
{
/* do stuff what this does it it makes a new entry into the JsonDataReleases
db using MyDate as the new LastUpdated value. */
}
/* Does more unrelated stuff... */
}
EDIT: So I figured it out!
Turns out that when the DateTime.Now was being saved as the LastUpdated value in the sql db, it cuts off the milliseconds.
var now = DateTime.Now;
var MyDate= new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
If your
LastUpdatedfield stores date & time, yourWhereclause will only select items with the same exact date & time of yourMyDate. If you’re just comparing dates, you need to compare the.Dateon each side: