【问题标题】:Compare DateTime in Sqlite with Entity and Linq将 Sqlite 中的 DateTime 与 Entity 和 Linq 进行比较
【发布时间】:2017-09-20 13:38:58
【问题描述】:

我正在尝试运行以下命令:

backupEvents = backupEvents.Where(e => e.Timestamp >= DateTime.Now - new TimeSpan(0, 1, 0, 0));

但这给了我一个错误提示

“DbArithmeticExpression 参数必须具有数字通用类型”。

搜索 StackOverflow,我发现 Arithmetic with DateTime is not supported in Entity Framework

所以我尝试了:

backupEvents = backupEvents.Where(o => DbFunctions.DiffHours(o.Timestamp, DateTime.Now) <= 1);

但我也不能使用提供的 DbFunctions 作为解决方案,因为我使用的是 SQLite,但它没有它们,我收到一条错误消息:“没有这样的功能:DiffHours”

那么 - 有没有办法在 SQLite 中使用实体来比较日期?

【问题讨论】:

    标签: c# linq sqlite datetime entity-framework-6


    【解决方案1】:

    找到了一种方法 - 这很有效:

    DateTime offsetHour = DateTime.Now.Add(new TimeSpan(-1, 0, 0));
    backupEvents = backupEvents.Where(e => e.Timestamp >= offsetHour);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-20
      • 2018-09-16
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多