7

I have something like the following:

new ObservableCollection<SomeData>(SomeDataCollection.Where(a => a.AGD_Category.Equals((int)AGD_CategoryEnum.Med) && ((a.AGG_DateStart.Date <= SelectedUntill) && (a.AGG_DateEnd.Value.Date >= SelectedFrom))));

now my a.AGG_DateEnd is a nullable datetime and whenever there is a null value in the list I get an exception because of the "a.AGG_DateEnd.Value.Date" part.

But the thing is, the a.AGG_DateEnd also contains hours and minutes, but I do not want this causes my comparison to give unreliable results.

What can I do to improve?

Thank you,

1
  • 1
    you could just add a null check inside your .Where(...). Nullable Types have a property called 'HasValue' which does exactly that. So a.AGG_DateEnd.HasValue should do the trick.
    – SKull
    Nov 26, 2014 at 8:28

1 Answer 1

21

A quick hack is:

a.AGG_DateEnd.GetValueOrDefault().Date >= SelectedFrom

This is OK because the default(DateTime) (which equals its own .Date) is not greater than any (other) DateTime value.

Otherwise the usual thing to do is:

a.AGG_DateEnd.HasValue && a.AGG_DateEnd.Value.Date >= SelectedFrom

where the double & ensures short-cut evaluation.


Addition after comments:

If you want the opposite answer in the case where it is null, it is:

(a.AGG_DateEnd ?? DateTime.MaxValue).Date >= SelectedFrom

respectively:

!a.AGG_DateEnd.HasValue || a.AGG_DateEnd.Value.Date >= SelectedFrom

Later edit: Since C# 6.0 (July 2015), you can use the ?. operator instead. So use the shorter code:

a.AGG_DateEnd?.Date >= SelectedFrom

which will (formally, at least) compare two nullable values of type DateTime?. When either operand is "null" (i.e. .HasValue gives false), such a comparison gives false.

If you want the opposite bool result in case of null, that is:

!(a.AGG_DateEnd?.Date < SelectedFrom)

Observe how x >= y is different from !(x < y) when we use these lifted operators that operate on Nullable<> values. Any lifted operator (like >= or <) will return false immediately if one of x and y is null; only if both x and y are non-null, will they be unwrapped and the contents compared with the non-lifted operator whose return value will be used.

3
  • I see... thanks for your answer, I will accept it.... Whenever a AGG_DateEnd is null, it will not end up in the resultset. Is it possible to use a fictional date if AGG_DateEnd is null like 31/12/4711? I mean to replace AGG_DateEnd... Nov 26, 2014 at 8:47
  • ... Never mind, just need to do this : a.AGG_DateEnd.GetValueOrDefault(new DateTime(4711, 12, 31)) >= SelectedFrom !! THANK YOU Nov 26, 2014 at 8:55
  • 1
    @user1841243 I extended that answer for that. Note that you can use (x ?? y) instead of x.GetValueOrDefault(y) if you prefer. Nov 26, 2014 at 9:00

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.