Conor8630
Conor8630

Reputation: 345

ASP.NET MVC ToString error when converting a DateTime to a month number

I am confused with the error I am getting. I am trying to return records that have the Month number equal to i but I recieve the following error.

    No overload for method 'ToString' takes 1 arguments 

Is there another way to return month numbers from this table

 var ColourDates = Model.Color.Any(e => e.StartDate.ToString("MM") == i);

Upvotes: 0

Views: 361

Answers (1)

OliveJuice
OliveJuice

Reputation: 368

When using Nullable types, you will need to get the value of the variable before use. You will need something like var ColourDates = Model.Color.Any(e => e.StartDate.Value.ToString("MM") == i);

NOTE: This does NOT handle null values. You will get an exception if e.StartDate.Value is null. I highly recommend using e.StartDate.HasValue to do a null check before use.

Upvotes: 2

Related Questions