Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

querying between two datetimes in linq-to-sql

Tags:

c#

linq

I have a query that takes 2 datetimes as parameters and I need to write a where clause to get the records that are in between. These are not just dates but dates with times (ie. 1/28/2012 9:45)

So far, I this:

where d.RecordDateTime > StartDate && d.RecordDateTime < EndDate

Should I be rewritting these as:

where d.RecordDateTime > StartDate.Date && d.RecordDateTime < EndDate.Date

or is it fine as is.

Thanks.

like image 379
frenchie Avatar asked Mar 16 '26 06:03

frenchie


1 Answers

Your current query definitley works... depending whether you want the result to include StartDate and/or EndDate it should be changed a little bit:

where d.RecordDateTime >= StartDate && d.RecordDateTime <= EndDate

IF you change the query as you proposed in your question then you would make it include all in the result independent of time - although it would miss rows with a time like 00:00:00 or even miss a whole day (the EndDate).

like image 67
Yahia Avatar answered Mar 17 '26 18:03

Yahia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!