我有一个这样的数据库字段:
Timestamp varchar(19) utf8mb4_unicode_ci
包含这样的时间戳(字符串)
"2013-05-29 00:00:00"
我正在使用 Entity Framework ,我想按时间过滤 - 这意味着我想获得所有具有时间戳 >(现在间隔)的条目。 代码看起来像这样
var query = from r in db.route
where
r.timestamp > (now-interval);
select r;
我该怎么做?
最佳答案
我的第一个建议是修复数据库,以便将日期值存储为正确的日期类型。这将解决许多问题并提高搜索性能。但是,在(不太可能)您无法做到这一点的情况下,并且该列中值的格式都与您在问题中指定的完全匹配,您可以将本地时间戳变量转换为字符串并进行比较直接地。由于您显示的格式具有与日期顺序相同的字母数字顺序,因此它应该可以工作:
//It's important that this is done here and not inline with the Linq query
var nowInterval = timeStamp.ToString("yyyy-MM-dd HH:mm:ss");
var query = from r in db.route
where string.Compare(r.timestamp, nowInterval, StringComparison.Ordinal) > 0
select r;
关于c# - 将 mysql 数据库中的字符串日期与 Entity Framework c# 进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33102385/