I am coding a MVC 5 internet application, and I am wanting to write a function that will work with both a SQL statement as well as with a List.
Here is my code:
public static class TestableDbFunctions
{
[System.Data.Entity.DbFunction("Edm", "DiffDays")]
public static double? DiffDays(DateTime? dateValue1, DateTime? dateValue2)
{
if (!dateValue1.HasValue || !dateValue2.HasValue)
return null;
return (double)((dateValue2.Value - dateValue1.Value).TotalDays);
}
}
As you can see, the code calculates the difference in days between two DateTimes
.
My question is this:
Should the code be
(dateValue2.Value - dateValue1.Value).TotalDays
or
(dateValue1.Value - dateValue2.Value).TotalDays?
Thanks in advance.
Since you are trying to compliant with SQL, the closest analogue is DateDiff. The 1st argument is the start date
and the 2nd i end date
. The code should be
return (endDate - startDate).TotalDays;