DbArithmeticExpression arguments must have a numeric common type

后端 未结 3 1860
终归单人心
终归单人心 2020-11-28 03:39
TimeSpan time24 = new TimeSpan(24, 0, 0);
TimeSpan time18 = new TimeSpan(18, 0, 0);    

// first get today\'s sleeping hours
List sleeps = contex         


        
3条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 03:54

    Arithmetic with DateTime is not supported in Entity Framework 6 and earlier. You have to use DbFunctions*. So, for the first part of your statement, something like:

    var sleeps = context.Sleeps(o =>
        DbFunctions.DiffHours(o.ClientDateTimeStamp, clientDateTime) < 24);
    

    Note that the DiffHours method accepts Nullable.

    Entity Framwork core (when used with Sql Server, maybe other db providers) supports the DateTime AddXxx functions (like AddHours). They're translated into DATEADD in SQL.

    *EntityFunctions prior to Entity Framework version 6.

提交回复
热议问题