0

I have two variables of type DateTime and DateTime?

DateTime StartDateFromDb;
DateTime? StartDateFromFilter;

if(StartDateFromDb.Date == StartDateFromFilter.Date);

While comparing, .Date is not allowingfor StartDateFromFilter of type allow null

Thanks in advance

Jinna Balu
  • 6,747
  • 38
  • 47

2 Answers2

3

Use the Value property available as

  if(StartDateFromFilter.HasValue && StartDateFromDb.Date == StartDateFromFilter.Value.Date)

PS: Better to add a null value check. StartDateFromFilter must have a value.(HasValue is true when DateTime? type variable is not null)

M.S.
  • 4,283
  • 1
  • 19
  • 42
2

For any nullable type , you can use value property. StartDateFromFilter.Value.Date

In your case , this should work fine

if(StartDateFromDb.Date == StartDateFromFilter.Value.Date)
//// in this case .Date is not allowingfor StartDateFromFilter
prabin badyakar
  • 1,636
  • 2
  • 16
  • 24