首页 > 解决方案 > 在 F# 中,表达式的类型应为“Nullable”' 但这里有类型 'DateTime'

问题描述

我在 F# 中有以下函数,它从本地数据库返回数据:

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

访问定义为:

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

现在从数据库下载的 q.birthdate 是非空的,但访问定义将其设置为可空。因此,以下错误结果:

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

如何在不更改数据库的情况下保持访问的定义相同并修复错误?

TIA

标签: f#

解决方案


您需要显式地创建一个Nullable像这样的值:Nullable q.birthdate.


推荐阅读