Alan Wayne
Alan Wayne

Reputation: 5394

In F#, The expression was expected to have type 'Nullable<DateTime>' but here has type 'DateTime'

I have the following function in F# which returns data from a local database:

let GetScheduleAsync (tableDate : DateTime) =
        async {
            let! data = context.GetOfficeScheduleAsync(tableDate) |> Async.AwaitTask
            return data |> Seq.map(fun q -> {
                Visit.lastName = q.lastname
                firstName = q.firstname
                birthDate = q.birthdate 
                appointmentTime = q.appointment_time
                tservice = q.service_time
                postingTime = q.posting_time
                chartNumber = q.chart_number
                })                  
          }
          |> Async.StartAsTask

Where Visit is defined as:

type Visit = {
    lastName : string
    firstName : string
    birthDate : Nullable<DateTime>
    appointmentTime : Nullable<DateTime>
    tservice : Nullable<DateTime>
    postingTime : Nullable<DateTime>
    chartNumber : Nullable<int>
    }

Now the downloaded q.birthdate from the database is non-null, but the Visit definition has it as nullable. Hence, the following error results:

The expression was expected to have type
  'Nullable<DateTime>'
but here has type
   'DateTime'

How do I keep the definition of Visit the same without changing the database and fix the error?

TIA

Upvotes: 2

Views: 143

Answers (1)

TheQuickBrownFox
TheQuickBrownFox

Reputation: 10624

You need to explicitly make a Nullable value like this: Nullable q.birthdate.

Upvotes: 5

Related Questions