Compare nullable datetime objects

我们两清 提交于 2019-11-30 01:26:56

问题


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.

I have also tried:

birthDate > hiredate...

But the results are not as expected...any suggestions?


回答1:


To compare two Nullable<T> objects use Nullable.Compare<T> like:

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

You can also do:

Use the Value property of the Nullable DateTime. (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

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)
{
}



回答2:


Nullable.Equals Indicates whether two specified Nullable(Of T) objects are equal.

Try:

if(birthDate.Equals(hireDate))

The best way would be: Nullable.Compare Method

Nullable.Compare(birthDate, hireDate));



回答3:


If you want a null value to be treated as default(DateTime) you could do something like this:

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

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);



回答4:


Use the Nullable.Compare<T> method. Like this:

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



回答5:


Try birthDate.Equals(hireDate) and do your stuff after comparision.
or use object.equals(birthDate,hireDate)



回答6:


As @Vishal stated, simply use overriden Equals method of Nullable<T>. 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. So, simply use

birthDate.Equals(hireDate)



回答7:


I think you could use the condition in the following manner

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



回答8:


You can write a generic method to calculate Min or Max for any type like below:

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();


来源:https://stackoverflow.com/questions/14251902/compare-nullable-datetime-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!