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.