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

2

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.