for getting date different in second for in memory collection use like this

var diffInSeconds = (dateTime1 - dateTime2).TotalSeconds;

function you are using is ment to work with sql database not with in memory collection.

its failing because you are relying value which float or decimal, i suggest you try something like Math.Round(val,2) and check result however comparing two decimal or float is always problem, better put value which will give you integer value without fraction part.

Below is one example of double comparison, this leads to ouput value false same thing is happing in your case.

// Initialize two doubles with apparently identical values
double double1 = .33333;
double double2 = 1/3;
// Compare them for equality
Console.WriteLine(double1.Equals(double2));    // displays false

you can check here : Double.Equals Method (Double)

I am taking aout this part of your code

TimeToPay = (empGroup.Where(x => x.EmployeeID == 
 empGroup.Key.EmployeeID).Sum(x => (SqlFunctions.DateDiff("second",
 x.PayTimeIn, x.PayTimeOut))/60.0/60.0)) ?? 0


You've mentioned LINQ to entities; as such, I'd recommend trying Dbfunctions in your tests and code instead (I've had this issue happen to me before and that switch made all the difference. Here's a related question which leads to other related questions: SqlFunctions vs DbFunctions)

However, DbFunctions doesn't have DateDiff, so you'll have to use DiffSeconds in your case.

Your line would thus change to:

TimeToPay = (empGroup.Where(x => x.EmployeeID == empGroup.Key.EmployeeID).Sum(x => (DbFunctions.DiffSeconds(x.PayTimeIn, x.PayTimeOut))/60.0/60.0)) ?? 0

Note: This will only work in tests where the query provider is adequately mocked out. Preferrable with Effort.EF6 (here's a post on how to plug it in), or EF Core's in-memory database. You could go about trying to mock your own provider, but that would be a terrible idea just to get these tests working.

Also, once you've switched to an adequate in-memory data context, instead of mocking out the Find() method, you'd save your list to the database and then query it as you normally would have.

Related Articles