【问题标题】:Daterange with startDate and endDate not working if both date same如果两个日期相同,则带有 startDate 和 endDate 的日期范围不起作用
【发布时间】:2017-02-20 18:01:11
【问题描述】:

我得到所选日期范围的返回数据,该日期范围介于或不在范围之间。但如果 startDate 和 endDate 相同,则不会。例如:从 14.10.2017 - 14.10.2017。它返回我然后没有数据(但它应该因为 5 个数据库记录受到影响)

foreach (Content content in db.Contents)
{
   if (content.ShippedDate < startDate || content.ShippedDate > endDate)
}

谁有解决办法?

【问题讨论】:

  • 日期范围的结束意味着是包含还是不包含?这是首先要做的决定。

标签: c# asp.net visual-studio


【解决方案1】:

您应该更新条件以包含相同的日期。

1.1.2016 < 1.1.2016   //FALSE
1.1.2016 <= 1.1.2016  //TRUE

调整条件:

content.ShippedDate <= startDate 
content.ShippedDate >= endDate

代码:

foreach (Content content in db.Contents)
{
    if (content.ShippedDate <= startDate || content.ShippedDate >= endDate)
        //the code here
}

【讨论】:

    【解决方案2】:

    使用&lt;=(小于或等于)和&gt;=(大于或等于)而不是&lt;&gt;,这将匹配相同的值,而不仅仅是更小的值或大于给定值。

    另外请参阅此页面以了解有关 c# 运算符的更多信息,因为这是一个非常基本的问题

    https://msdn.microsoft.com/en-us/library/6a71f45d.aspx

    【讨论】:

      猜你喜欢
      • 2021-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多