【问题标题】:Compare nullable datetime objects比较可为空的日期时间对象
【发布时间】:2013-01-10 06:18:11
【问题描述】:

我有两个可以为空的日期时间对象,我想比较两者。最好的方法是什么?

我已经试过了:

DateTime.Compare(birthDate, hireDate);

这给出了一个错误,它可能需要 System.DateTime 类型的日期,而我有 Nullable 日期时间。

我也试过了:

birthDate > hiredate...

但结果并不如预期...有什么建议吗?

【问题讨论】:

  • 是否为空
  • 使用birthDate > hireDate 时的结果与您的预期有何不同,因为这是最简单的方法。 C# 引入了提升的运算符,让您的操作变得简单。

标签: c# datetime compare nullable


【解决方案1】:

要比较两个 Nullable<T> 对象,请使用 Nullable.Compare<T>,例如:

bool result = Nullable.Compare(birthDate, hireDate) > 0;

你也可以这样做:

使用 Nullable DateTime 的 Value 属性。 (记得检查两个对象是否都有一些值)

if ((birthDate.HasValue && hireDate.HasValue) 
    && DateTime.Compare(birthDate.Value, hireDate.Value) > 0)
{
}

如果两个值都相同 DateTime.Compare 将返回您0

类似的东西

DateTime? birthDate = new DateTime(2000, 1, 1);
DateTime? hireDate = new DateTime(2013, 1, 1);
if ((birthDate.HasValue && hireDate.HasValue) 
    && DateTime.Compare(birthDate.Value, hireDate.Value) > 0)
{
}

【讨论】:

  • 如果 nullable 没有值,你会在尝试访问 Value 属性时遇到异常
  • 每种情况下会返回什么整数值,即 >
  • 如果任一日期为 null,.Value 将为 null 并抛出 InvalidOperationException 并带有“Nullable object must have a value”。 OP 可能希望在其中添加空检查。
  • @lazyberezovsky 那么你认为最好的方法是什么?因为我确实有值可以为空的情况。
  • var equal = Nullable<DateTime>.Compare(birthDate, hireDate);
【解决方案2】:

Nullable.Equals 表示两个指定的 Nullable(Of T) 对象是否相等。

试试:

if(birthDate.Equals(hireDate))

最好的方法是:Nullable.Compare Method

Nullable.Compare(birthDate, hireDate));

【讨论】:

    【解决方案3】:

    如果您希望将 null 值视为 default(DateTime),您可以执行以下操作:

    public class NullableDateTimeComparer : IComparer<DateTime?>
    {
        public int Compare(DateTime? x, DateTime? y)
        {
            return x.GetValueOrDefault().CompareTo(y.GetValueOrDefault());
        }
    }
    

    并像这样使用它

    var myComparer = new NullableDateTimeComparer();
    myComparer.Compare(left, right);
    

    另一种方法是为值可比较的Nullable 类型创建一个扩展方法

    public static class NullableComparableExtensions
    {
        public static int CompareTo<T>(this T? left, T? right)
            where T : struct, IComparable<T>
        {
            return left.GetValueOrDefault().CompareTo(right.GetValueOrDefault());
        }
    }
    

    你会在哪里这样使用它

    DateTime? left = null, right = DateTime.Now;
    left.CompareTo(right);
    

    【讨论】:

      【解决方案4】:

      使用Nullable.Compare&lt;T&gt; 方法。像这样:

      var equal = Nullable.Compare<DateTime>(birthDate, hireDate);
      

      【讨论】:

        【解决方案5】:

        正如@Vishal 所说,只需使用Nullable&lt;T&gt; 的覆盖Equals 方法。它是这样实现的:

        public override bool Equals(object other)
        {
            if (!this.HasValue)    
                return (other == null);
        
            if (other == null)    
                return false;
        
            return this.value.Equals(other);
        }
        

        如果两个可空结构都没有值,或者它们的值相等,则返回true。所以,只需使用

        birthDate.Equals(hireDate)
        

        【讨论】:

          【解决方案6】:

          我认为您可以按以下方式使用条件

          birthdate.GetValueOrDefault(DateTime.MinValue) > hireddate.GetValueOrDefault(DateTime.MinValue)
          

          【讨论】:

            【解决方案7】:

            试试

            birthDate.Equals(hireDate)
            

            比较后做你的事情。

            或者,使用

            object.equals(birthDate,hireDate)
            

            【讨论】:

              【解决方案8】:

              您可以编写一个通用方法来计算任何类型的最小值或最大值,如下所示:

              public static T Max<T>(T FirstArgument, T SecondArgument) {
                  if (Comparer<T>.Default.Compare(FirstArgument, SecondArgument) > 0)
                      return FirstArgument;
                  return SecondArgument;
              }
              

              然后像下面这样使用:

              var result = new[]{datetime1, datetime2, datetime3}.Max();
              

              【讨论】:

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