0

我正在尝试使用 VB 进行连接查询 Linq。我想返回最大 EnterTime,但出现错误,说“

无法将 System.Linq.Iqueryable(Of Date) 转换为 System.Nullable(Of Date)

我该如何解决这个问题?

     Dim results =
            From b In context.H_Beneficiary
            Join e In context.Employees On b.EmployeeId Equals (e.EmployeeId)
            Join c In context.Companies On e.CompanyId Equals (c.CompanyId) _
            Where e.IntegrationId.Equals(c.CompanyId) _
            And (b.EndDate Is Nothing Or b.EndDate >= DateTime.Today)
            Select b.EnterTime
4

1 回答 1

1

您的 Linq 查询返回日期序列,而不是单个日期。这就是为什么任务不起作用的原因。

要从查询中分配最大日期,您可以按日期对查询进行排序,而不是选择第一个结果(如果有):

Dim results =
        From b In context.H_Beneficiary
        Join e In context.Employees On b.EmployeeId Equals (e.EmployeeId)
        Join c In context.Companies On e.CompanyId Equals (c.CompanyId) _
        Where e.CompanyIntegrationId.Equals(c.CompanyId) _
        And (b.EndDate Is Nothing Or b.EndDate >= DateTime.Today)
        Order By b.EnterTime Descending
        Select b.EnterTime

Dim latestDate As Nullable(Of DateTime) = results.FirstOrDefault()
于 2012-09-14T15:48:14.843 回答