QueryExpression之GreaterEqual和LessEqual

1. QueryExpression
Conditions =
{
new ConditionExpression("createdon",ConditionOperator.GreaterEqual,DateTime.Parse("2020-4-10").Date),
new ConditionExpression("createdon",ConditionOperator.LessEqual,DateTime.Parse("2020-4-14").Date)
}
  • GreaterEqual:大于等于,但要注意此处的DateTime.Parse("2020-4-10").Date其实等于{4/10/2020 12:00:00 AM},也就是说创建时间在4/10号这一天的统计在内
  • LessEqual:小于等于,但要注意此处的DateTime.Parse("2020-4-14").Date其实等于{4/14/2020 12:00:00 AM},也就是说创建时间在4/14号这一天的不统计在内,统计范围其实是到{4/13/2020 11:59:59 999 PM}

备注:比较日期时这两个操作符是精确到时分秒的

2. FetchXML
<filter type="and">
  <condition attribute="createdon" operator="on-or-after" value="2020-04-10" />
  <condition attribute="createdon" operator="on-or-before" value="2020-04-14" />
</filter>
  • on-or-after:晚于(包含当天),也就是说创建时间在4/10号这一天的也统计在内
  • on-or-before:早于(包含当天),也就是说创建时间在4/14号这一天的也统计在内

备注:比较日期时这两个操作符只精确到天

误区解读:

关于DateTime.Now.Date,之前一直以为会返回一个2020-04-14这种(yyyy-MM-dd)格式的日期,但其实他返回的也是具有时分秒的,是这一天的开始时分秒,即凌晨0点{4/14/2020 12:00:00 AM}
如果想要“yyyy-MM-dd”这种格式,需要使用DateTime.Now.ToString("yyyy-MM-dd"),返回的是一个string类型。
备注:如果想要当月指定的一天,可以试用DateTime.Now.ToString("yyyy-MM-01"),返回"2020-04-01",也就是当月的1号

原文地址:https://www.cnblogs.com/cndota2/p/12696838.html