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
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)
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.
- linq to entity datediff fails unit test, but not sql data
- Encrypting Decrypting Data via Entity Framework using separate class exposing encrypted/decrypted values: Linq statements fails
- LINQ entity data model generated code error - The type 'DBContexts.Category' already contains a definition for 'ID'
- how to select data by linq in many-to-many relationship in First code Entity framework 5
- c# Linq or code to extract groups from a single list of source data
- How are people unit testing code that uses Linq to SQL
- Unit test error : This function can only be invoked from LINQ to Entities
- LINQ to Entities only supports casting Entity Data Model primitive types?
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- How to unit test Entity Framework?
- How does linq actually execute the code to retrieve data from the data source?
- LINQ Source Code Available
- Merge duplicate data without affecting others in LINQ code
- Use a linq query as microsoft local report Data Source (WinForms)
- Linq Unit Test Suite
- Select data from multiple unrelated tables with LINQ to Entity Framework
- At what point is a LINQ data source determined?
- Expression... LINQ to Entities only supports casting Entity Data Model primitive types”
- creating Linq to sqlite dbml from DbLinq source code
- Stubbing Code for Test With Linq Expressions and Lambdas
- What are the major differences between Data Access Application Block, NHibernate, ADO.NET Entity Framework and LINQ to SQL?
- Pull data from multiple tables in one SQL query using LINQ and Entity Framework (Core)
- Using Linq to Entity for searching large data cause high memory usage
- read icollection data using LINQ in C# code
- Entity Framework LINQ projection into custom type results in missing data
- LINQ to Entities error occurring at run time but not in unit test
- updating data in many-to-many relationship in entity framework in code first existing database
- Working LINQ query does not work in unit test
- Linq to sql as object data source - designer problem with partial classes
- C# LINQ How to get a data source from a db?
- Group by and then select based on those group values
- How to convert foreach loop with a switch statement into LINQ
- LINQ JOIN + GROUP BY + SUM
- Is there any difference in execution for async lambda?
- I have never used JOIN. When is proper time?
- How to query a sitemap using an XDocument linq query
- The cast to value type 'double' failed because the materialized value is null - AVERAGE
- How Order by Case in LINQ
- Razor LabelFor and DropdownFor
- How to implement LessThan, etc., when building expressions on strings
- How can I return a Dictionary<int, object>?
- LINQ Select analogue for async method
- Is saving to database just to get an ID a bad hack?
- Finding nodes with "missing" attributes
- LinqToSql: ExecuteQuery an ExecuteQuery-result?
- How to reuse a data into an anonymous type decalaration?
- Setting up linq to sql context
- Why does not entity framework use the like in this LinQ subquery?
- How to limit a LINQ query based on date and a secondary ID
- Extension method in a where clause