Click here to Skip to main content
15,906,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have employee salary modification history

and need to find what is the salary for the searched period for example

salary have been modified in the following dates

08/03/2017 it was 2200 US and became 2300
24/12/2018 it was 2300 US and became 2400
25/12/2018 it was 2400 US and became 2300
17/02/2019 it was 2300 US and became 2400


if i searched for salary at the period from 26/01/2017 to 25/02/2017 i expect the result to be 2200

if i searched for salary at the period from 26/02/2017 to 25/03/2017 i expect the result to be 2300

if i searched for salary at the period from 26/11/2018 to 25/12/2017 i expect the result to be 2400

if i searched for salary at the period from 26/12/2018 to 25/01/2019 i expect the result to be 2300



the problem with my c# code that it return the whole dates from 08/03/2017 to 17/02/2019

at any period from the searched periods above

i need the above expected results what can i do to obtain correct result



What I have tried:

C#
var dbitem = db.tblSalaryHistories.Where(c => c.Employee_ID == employeeId && (
                         (startDate <= c.Update_Date && endDate >= c.Update_Date) ||
                         (startDate >= c.Update_Date && endDate <= c.Update_Date) ||
                         (startDate <= c.Update_Date) ))
Posted
Updated 10-Mar-19 1:52am

1 solution

The problem is that your code does exactly what you ask of it: returns all data.
This is because you have OR conditions which amount to:
(a <= b && c >= b) || (a >= b && c <= b) || (a <= b)

So, if at any time, a < b, the whole condition is true. The first condition is irrelevant because the third will always be true when the first is.

So if you search for a startDate of 26-01-2017, if will automatically match all rows in your sample data, and return them.

If you provide an order - perhaps by c.UpdateDate ascending, you could use FirstOrDefault instead of Where to return a single row, but even then, I think you want to look rather more closely at your condition and probably reduce it to
C#
startDate <= c.Update_Date && endDate >= c.Update_Date
 
Share this answer
 
Comments
TheSniper105 10-Mar-19 8:01am    
startDate <= c.Update_Date && endDate >= c.Update_Date
will fall if the search peroid is from 26/01/2017 to 25/02/2017 will return nothing as the first modification is 08/03/2017
i expect it to return the oldSalary before modification in 08/03/2017 wich is 2200

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900