在Linq Join Query中选择Max Date

时间:2012-09-14 14:52:10

标签: vb.net linq inner-join max

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

  

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

我该如何解决这个问题?

     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

1 个答案:

答案 0 :(得分:1)

You 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()