Oh! I misunderstood as I thought that they were nullable DateTime objects. Since that is the case then you will need to pull the DateTime related to the offset prior to comparing them :
You may have to convert both of these values to UTC to handle the comparison between the offset (including the proper offset time) and the UTC version of your current time :
//Comparing UTC Versions of each of your Dates and Offsets
s.StartDate.UtcDateTime > startDate.ToUniversalTime()
or you might even be able to use the Ticks property of each :
Are they nullable Date Times or just normal Date Times? For nullables, you can use the
.HasValue property, otherwise just perform the normal null checking (startDate != null)
nissan
Participant
1065 Points
618 Posts
Comparing null dates
Jan 29, 2013 06:03 PM|LINK
I am doing something like this..
return _EventRepository .GetActiveEvents(locationId) .Where( s=> (s.StartDate >= startDate) && ( s.EndDate < endDate)); }But startDate and endDate are nullable(DateTime?).. and I get this error..
The operands for operator 'GreaterThanOrEqual' do not match the parameters of method 'op_GreaterThanOrEqual'.
How can I fix this?
Rion William...
All-Star
27590 Points
4561 Posts
Re: Comparing null dates
Jan 29, 2013 06:29 PM|LINK
If you are using nullable DateTime objects (DateTime?) then you will need to use the Value property of the object :
so you should be able to use something like this :
return _EventRepository.GetActiveEvents(locationId) .Where( s=> (s.StartDate.Value >= startDate) && ( s.EndDate.Value < endDate)); }if you are using those with nullable values, you may also want to check if they have a value prior to comparing them as well :
return _EventRepository.GetActiveEvents(locationId) .Where( s=> (s.StartDate.HasValue && s.StartDate.Value >= startDate) && ( s.EndDate.HasValue && s.EndDate.Value < endDate)); }nissan
Participant
1065 Points
618 Posts
Re: Comparing null dates
Jan 29, 2013 07:24 PM|LINK
my S.StartDate does not have a property of HasValue or Value.. When I do dot.. those doesn't come up in the intellisense.
UPDATE: BTW, s.StartDate is a DateTimeOffset and startDate is a DataTime?
Rion William...
All-Star
27590 Points
4561 Posts
Re: Comparing null dates
Jan 29, 2013 07:32 PM|LINK
Oh! I misunderstood as I thought that they were nullable DateTime objects. Since that is the case then you will need to pull the DateTime related to the offset prior to comparing them :
return _EventRepository.GetActiveEvents(locationId) .Where( s=> (s.StartDate.DateTime >= startDate) && ( s.EndDate.DateTime < endDate)); }You may have to convert both of these values to UTC to handle the comparison between the offset (including the proper offset time) and the UTC version of your current time :
or you might even be able to use the Ticks property of each :
nissan
Participant
1065 Points
618 Posts
Re: Comparing null dates
Jan 29, 2013 07:49 PM|LINK
I changed startDate & endDate also to DateTimeOffset..
public IQueryable<Event> GetActiveEvents(int locationId, DateTimeOffset startDate, DateTimeOffset endDate) { return _EventRepository .GetActiveEvents(locationId) .Where( s=> (s.StormStartDate >= startDate) && ( s.StormEndDate < endDate)); }Now this code works.. but how can I check for nulls on startDate and endDate?
Rion William...
All-Star
27590 Points
4561 Posts
Re: Comparing null dates
Jan 29, 2013 07:55 PM|LINK
Are they nullable Date Times or just normal Date Times? For nullables, you can use the .HasValue property, otherwise just perform the normal null checking (startDate != null)
public IQueryable<Event> GetActiveEvents(int locationId, DateTimeOffset startDate, DateTimeOffset endDate) { return _EventRepository .GetActiveEvents(locationId) .Where( s=> (startDate != null && s.StormStartDate >= startDate) && (endDate != null && s.StormEndDate < endDate)); }or using .HasValue :
public IQueryable<Event> GetActiveEvents(int locationId, DateTimeOffset startDate, DateTimeOffset endDate) { return _EventRepository .GetActiveEvents(locationId) .Where( s=> (startDate.HasValue && s.StormStartDate >= startDate) && (endDate.HasValue && s.StormEndDate < endDate)); }nissan
Participant
1065 Points
618 Posts
Re: Comparing null dates
Jan 29, 2013 08:47 PM|LINK
when I do startDate != null, it shows in blue and says expression is always true...
Rion William...
All-Star
27590 Points
4561 Posts
Re: Comparing null dates
Jan 29, 2013 08:57 PM|LINK
If it is a DateTime field then that is true - as it can never be null. You may not need the null checking if that is the case for those values.
thaicarrot
Contributor
5132 Points
1465 Posts
Re: Comparing null dates
Jan 30, 2013 04:31 PM|LINK
return _EventRepository .GetActiveEvents(locationId) .Where( s=> (s.StartDate >= dateOne) && ( s.EndDate < dateTwo)); }Weera