简体   繁体   中英

querying between two datetimes in linq-to-sql

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.

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 ).

您需要检查此链接,因为它将告诉您日期时间格式的 where子句Where子句中过滤数据的简化方法

StartDtm < @ToTime  AND EndDtm >= @FromTime 

如果您有单个日期变量,并且在模型上有RecordDateStart和RecordDateEnd,则可以执行以下操作:

 .Where(x => x.DtStart <= YourDate & x.DtEnd >= YoutDate)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM