[英]linq where clause with 2 datetime conditions
I have a Where clause that looks like this: 我有一个Where子句,看起来像这样:
where p.Appoint.Date > TheStartTime.Date && p.Appoint.Date < TheEndTime.Date
For some reason, it's returning 0. However, if I write this 由于某种原因,它返回0。但是,如果我写这个
where p.Appoint.Date == TheStartTime.Date
it returns a count (not the one I want though). 它返回一个计数(虽然不是我想要的)。 What am I doing wrong? 我究竟做错了什么?
Thanks for your suggestions. 感谢您的建议。
When doing things like this it's always easier to use Ticks 当做这样的事情时,使用Ticks总是更容易
that way you're comparing between two numbers. 这样,您就可以在两个数字之间进行比较。
ok, I got it: 好,我知道了:
a) use >= and <=
b) remove the .Date
Your first clause has exclusive upper and lower bounds. 您的第一个子句具有互斥的上限和下限。 So for example if your lower date is 8:30 AM, this will not match records with an exact time of 8:30 AM, only those later than that. 因此,例如,如果您的下限日期为上午8:30,则此日期将与确切时间为上午8:30的记录不匹配,仅与晚于该时间的记录匹配。
Your second clause is looking for an exact match. 您的第二个子句正在寻找完全匹配。 If you want inclusive bounds, go with >=
and <=
instead of >
and <
. 如果要包含边界,请使用>=
和<=
代替>
和<
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.