1

我正在尝试检查 aDateTime是否已经在我的数据库中。

我已经分解了下面的代码,以便更好地了解正在发生的事情,但是尽管我尽了最大努力,但 dateChecks 将始终返回一个空列表。

    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... */

    }

编辑:所以我想通了!

事实证明,当 DateTime.Now 被保存为 sql 数据库中的 LastUpdated 值时,它会切断毫秒。

        var now = DateTime.Now;
        var MyDate= new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
4

1 回答 1

1

如果您的LastUpdated字段存储日期和时间,则您的Where子句将仅选择与您的MyDate. 如果您只是比较日期,则需要比较 .Date每一侧的日期:

var dateChecks = checkIfAlreadyPulled.
    Where(x => x.LastUpdated.Date == MyDate.Date).ToList();
于 2012-09-05T19:43:23.877 回答