I have a SQL query which is as follows:
SELECT [ClientId]
,[LastDelivery]
,[LastRequisitionDate]
,Datediff (day, LastRequisitionDate, LastDelivery) as DiffDate
FROM [dbo].[App_Client]
where (LastDelivery != '1900-01-01 00:00:00'
and LastRequisitionDate != '1900-01-01 00:00:00')
and Inactive = 0
and (Datediff (day, LastRequisitionDate, LastDelivery) < 9)
and (Datediff (day, LastRequisitionDate, LastDelivery) >= 0)
I have a list of clients, and I want all clients who received their delivery within 8 days of their requisition. The reason for the
LastDelivery != '1900-01-01 00:00:00'
and LastRequisitionDate != '1900-01-01 00:00:00'
is because the way I have my c# query requires that there are no null fields in any of the date fields(those fields are nullable in the DB but they really shouldn't be). I have a DB containing 11838 clients, and this query returns 10404. My problem is I have been unable to duplicate this query with C# linq.
My C# query is as follows:
var clients = _clientService.GetAllClients().Where(x =>
(x.LastDelivery != Convert.ToDateTime("01/01/1900")
&& x.LastRequisitionDate != Convert.ToDateTime("01/01/1900"))
&& x.Inactive == 0
&& (((DateTime)x.LastDelivery - (DateTime)x.LastRequisitionDate).Days < 9)
&& (((DateTime)x.LastDelivery - (DateTime)x.LastRequisitionDate).Days >= 0)).ToList();
This query returns 10563 results, a difference of 159, and I cannot figure out at all where I'm going wrong. To me that C# query looks identical to the SQL one, but somewhere there is a discrepancy. I've been coding all day, so maybe I'm a bit burnt out and the solution is obvious, but I just can't see it. Can anyone help, or suggest what I may be doing wrong or overlooking?
Answer
As correctly pointed below by Matt Smith, it turns out both queries were correct - the discrepancy lay in the SQL DateDiff function, which measures 1 day as when the day passes midnight, which means comparing 01/01/2016 23:59:59 and 01/02/2016 00:00:01 gives a difference of one day, whereas in my C# query, it was comparing actual difference in days as a timespan (24hrs). Great find and important distinction to be made, thanks to Matt Smith.
IEnumerablebecause you do stuff that would not be allowed in EF, because of this your database is doing a select with no filter, returning all rows, then you are filtering in memory which is going to give you bad performance.