[英]Compare nullable datetime objects
I have two nullable datetime objects, I want to compare both.我有两个可为空的日期时间对象,我想比较两者。 What is the best way to do it?最好的方法是什么?
I have already tried:我已经尝试过:
DateTime.Compare(birthDate, hireDate);
This is giving an error, maybe it is expecting dates of type System.DateTime
and I have Nullable datetimes.这给出了一个错误,也许它需要System.DateTime
类型的日期,而我有 Nullable 日期时间。
I have also tried:我也试过:
birthDate > hiredate...
But the results are not as expected...any suggestions?但结果并不如预期......有什么建议吗?
To compare two Nullable<T>
objects use Nullable.Compare<T>
like:要比较两个Nullable<T>
对象,请使用Nullable.Compare<T>
例如:
bool result = Nullable.Compare(birthDate, hireDate) > 0;
You can also do:你也可以这样做:
Use the Value property of the Nullable DateTime.使用 Nullable DateTime 的 Value 属性。 (Remember to check if both object Has some values) (记得检查两个对象是否都有一些值)
if ((birthDate.HasValue && hireDate.HasValue)
&& DateTime.Compare(birthDate.Value, hireDate.Value) > 0)
{
}
If both values are Same DateTime.Compare will return you 0
如果两个值都相同 DateTime.Compare 将返回0
Something Like就像是
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.Equals Indicates whether two specified Nullable(Of T) objects are equal. Nullable.Equals指示两个指定的 Nullable(Of T) 对象是否相等。
Try:尝试:
if(birthDate.Equals(hireDate))
The best way would be: Nullable.Compare Method最好的方法是: Nullable.Compare 方法
Nullable.Compare(birthDate, hireDate));
If you want a null
value to be treated as default(DateTime)
you could do something like this:如果您希望将null
值视为default(DateTime)
您可以执行以下操作:
public class NullableDateTimeComparer : IComparer<DateTime?>
{
public int Compare(DateTime? x, DateTime? y)
{
return x.GetValueOrDefault().CompareTo(y.GetValueOrDefault());
}
}
and use it like this并像这样使用它
var myComparer = new NullableDateTimeComparer();
myComparer.Compare(left, right);
Another way to do this would be to make an extension method for Nullable
types whose values are comparable另一种方法是为值可比较的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());
}
}
Where you'd use it like this你会在哪里使用它
DateTime? left = null, right = DateTime.Now;
left.CompareTo(right);
Use the Nullable.Compare<T>
method.使用Nullable.Compare<T>
方法。 Like this:像这样:
var equal = Nullable.Compare<DateTime>(birthDate, hireDate);
As @Vishal stated, simply use overriden Equals method of Nullable<T>
.正如@Vishal 所说,只需使用Nullable<T>
重写Equals方法。 It is implemented this way:它是这样实现的:
public override bool Equals(object other)
{
if (!this.HasValue)
return (other == null);
if (other == null)
return false;
return this.value.Equals(other);
}
It returns true
if either both nullable structs do not have value, or if their values are equal.如果两个可为空的结构都没有值,或者它们的值相等,则返回true
。 So, simply use所以,只需使用
birthDate.Equals(hireDate)
Try尝试
birthDate.Equals(hireDate)
and do your stuff after comparison.并在比较后做你的事情。
Or, use或者,使用
object.equals(birthDate,hireDate)
我认为您可以按以下方式使用该条件
birthdate.GetValueOrDefault(DateTime.MinValue) > hireddate.GetValueOrDefault(DateTime.MinValue)
You can write a generic method to calculate Min or Max for any type like below:您可以编写一个通用方法来计算任何类型的 Min 或 Max,如下所示:
public static T Max<T>(T FirstArgument, T SecondArgument) {
if (Comparer<T>.Default.Compare(FirstArgument, SecondArgument) > 0)
return FirstArgument;
return SecondArgument;
}
Then use like below:然后使用如下:
var result = new[]{datetime1, datetime2, datetime3}.Max();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.