检查日期是否等于linq到ef

时间:2012-09-05 19:00:32

标签: c# linq entity-framework

我正在尝试检查我的数据库中是否已有DateTime

我已经打破了下面的代码,以便更好地了解正在发生的事情但是尽管我付出了最大的努力,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 db中的LastUpdated值时,它会切断毫秒。

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

1 个答案:

答案 0 :(得分:1)

如果您的LastUpdated字段存储日期&时间,您的Where子句只会选择具有相同日期和时间的项目。 MyDate的时间。如果您只是比较日期,则需要比较每一方的.Date

var dateChecks = checkIfAlreadyPulled.
    Where(x => x.LastUpdated.Date == MyDate.Date).ToList();