DbArithmeticExpression arguments must have a numeric common type

后端 未结 3 1850
终归单人心
终归单人心 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 04:14

    I know that this is an old question but in your specific case instead of using DBFunctions as suggested by @GertArnold , couldn't you just invert the operation move out the arithmetic in question from the Lambda?

    After all clientDateTime and time24 are fix values so their difference does not need to be recalculated in every iteration.

    Like:

    TimeSpan time24 = new TimeSpan(24, 0, 0);
    TimeSpan time18 = new TimeSpan(18, 0, 0);    
    
    var clientdtminus24 = clientDateTime - time24;
    
    // first get today's sleeping hours
    List sleeps = context.Sleeps.Where(
        o => (clientdtminus24 < o.ClientDateTimeStamp) && 
              o.ClientDateTimeStamp.TimeOfDay > time18 && 
              clientDateTime.TimeOfDay < time18 && 
              o.UserID == userid).ToList();
    

    This refactor is usually possible if you are trying to compare the stored datetime shifted by a fix timestamp with an other datetime.

提交回复
热议问题