Arguments to DbArithmeticExpression must be of a numeric common type.

ado.net c# entity-framework

Question

TimeSpan time24 = new TimeSpan(24, 0, 0);
TimeSpan time18 = new TimeSpan(18, 0, 0);    

// first get today's sleeping hours
List<Model.Sleep> sleeps = context.Sleeps.Where(
    o => (clientDateTime - o.ClientDateTimeStamp < time24) && 
          o.ClientDateTimeStamp.TimeOfDay > time18 && 
          clientDateTime.TimeOfDay < time18 && 
          o.UserID == userid).ToList(); 

This Linq statement raises the following exception:

DbArithmeticExpression arguments must have a numeric common type.

Please Offer Your Assistance!

1
117
8/4/2012 4:43:38 PM

Accepted Answer

mathematics withDateTime is incompatible with Entity Framework versions 6 and older. You must enter DbFunctions*. Therefore, use the following as the first portion of your statement:

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

To be aware of, the DiffHours techniqueNullable<DateTime> .

Core of entity framework (when used with Sql Server, maybe other db providers) accommodates the DateTimeAddXxx functions (such asAddHours They are interpreted asDATEADD in SQL.

Before Entity Framework version 6, it was *EntityFunctions.

239
12/14/2017 11:03:38 AM

Popular Answer

Although this is an old query, in your particular situation, employingDBFunctions Couldn't you just reverse the procedure and transfer the problematic arithmetic outside of the Lambda, as proposed by @GertArnold?

In any caseclientDateTime and time24 are fixed values, hence the calculation of their change between iterations is not necessary.

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<Model.Sleep> sleeps = context.Sleeps.Where(
    o => (clientdtminus24 < o.ClientDateTimeStamp) && 
          o.ClientDateTimeStamp.TimeOfDay > time18 && 
          clientDateTime.TimeOfDay < time18 && 
          o.UserID == userid).ToList();

When attempting to compare a recorded datetime that has been displaced by a fixed timestamp with another datetime, this refactor is often feasible.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow