Finding Max Date using Entity Framework Core - in Razor Pages

entity-framework-core razor-pages

Question

I am building a Razor Pages web application that would register a user into an exam. The ExamRegistration Model has a field called MaxDateForExam that can be nullable as this is a new feature added to existing database at a later time. When creating the exam I want to fill this field with a value after the OnPost.

If the user is applying for the exam for the first time, simply get the ExamDate and add 1 year. On subsequent registrations (user failed exam) I do not wish to create a new MaxDate but grab the MaxDate from the first time registering record.

My ExamRegistrations model has the following property

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? MaxDateForExam { get; set; }

On the page where I would register a person into a new exam I need to perform a check:

  • If the person is registering the 1st time I would grab the ExamDate and add a year to it which would then populate a MaxDateForExam field.

  • If the user is registering for an exam which is not a 1st time I need to have the app go into the ExamRegistration table and grab the
    existing MaxDateForExam a value for the previous registration (if
    one exists). This is what I have so far.

I am just unable to find the correct code to extract the value of MaxDateForExam. Here is part of my code....

public DateTime MaxWriteDate { get; set; }                 
public Models.ExamRegistration ApplicantRecord { get; set; }

if(ExamRegistration.ExamAttempt == "1st Attempt")
     {                    
        MaxWriteDate = ExamRegistration.Exam.ExamDate.AddYears(1);
     }
     else
     {
         ApplicantRecord = _context.ExamRegistrations                        
         .Where(m => m.ApplicantID == ExamRegistration.ApplicantID && m.ExamAttempt == "1st Attempt") 
         .FirstOrDefault();

         if(ApplicantRecord == null)
          {
              MaxWriteDate = ExamRegistration.Exam.ExamDate.AddYears(1);
          }
          else
          {
               MaxWriteDate = ApplicantRecord.MaxDateForExam;   ???????FAILS????????
          }
      }

Any direction I can get would be helpful. The error always seems to revolve around the unable to convert System.DateTime? to System.DateTime. Are you missing a cast.

I have mark an answer as correct and I have learned a bit more of C# and nullable types. Thanks.

I was able to get past the error by using the code:

MaxWriteDate = OldMaxWriteDate.MaxDateForExam ?? DateTime.MinValue;

Thanks for the help.

PS.... If I need to mark something else (first time user to the great resource) please let me know.

Thanks

1
0
3/30/2020 1:11:41 PM

Accepted Answer

This is because the value of MaxDateForExam might by null, so you are not allowed to assign it to a variable that is not nullable. Try this:

      if(ApplicantRecord == null)
      {
          MaxWriteDate = ExamRegistration.Exam.ExamDate.AddYears(1);
      }
      else
      {
          if (MaxDateForExam.HasValue)
          {
              MaxWriteDate = ApplicantRecord.MaxDateForExam.Value;
          }
          else
          {
              // Handle case where MaxDateForExam does not have a value
          }
      }

See the nullable value types documentation for more information:

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-value-types

0
3/27/2020 8:51:34 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow