如何从另一个也是集合的集合中的项目中获取集合

问题描述 投票:0回答:2

我会需要:

  1. 包含所有公司所有员工的list(of employee)。 (e1,e2 ... e9)
  2. 员工最早的DischargeDate如何高于1/1/2018

我已经用这种方式解决了它,但我认为它可以以更清晰,更有效的方式实现。也许使用Linq或/和一些lambda表达式

1:

     Dim allEmp As New List(Of employee)
        For Each co In Comps
            For Each emplo In co.emp
                allEmp.Add(emplo)
            Next
        Next

2:

    Dim min As Date = Nothing
        For Each c In Comps
            For Each em In c.emp
                If min = Nothing OrElse (em.DischargeDate > New Date(2018, 1, 1) AndAlso em.DischargeDate < min) Then
                    min = em.DischargeDate
                End If
            Next
        Next

我也试过了

Dim xxx As List(Of List(Of employee)) = Comps.Select(Function(j) j.emp).ToList

但通过这种方式,我获得了List(Of List(Of employee))而不是List(Of employee)

这些是课程

Public Class company
        Public name As String
        Public emp As List(Of employee)

    End Class

    Public Class employee
        Public name As String
        Public email As String
        Public DischargeDate As Date

    End Class

这里是收藏品

        Private Comps As New List(Of company)

        Dim e As List(Of employee)

        Dim e1 As New employee With {.email = "[email protected]", .name = "nameEmp1", .DischargeDate = New Date(2019, 1, 12)}
        Dim e2 As New employee With {.email = "[email protected]", .name = "nameEmp2", .DischargeDate = New Date(2018, 8, 24)}
        Dim e3 As New employee With {.email = "[email protected]", .name = "nameEmp3", .DischargeDate = New Date(2017, 5, 12)}
        e = New List(Of employee) From {{e1}, {e2}, {e3}}
        Comps.Add(New company With {.name = "nameCom1", .emp = e})


        Dim e4 As New employee With {.email = "[email protected]", .name = "nameEmp4", .DischargeDate = New Date(2014, 3, 22)}
        Dim e5 As New employee With {.email = "[email protected]", .name = "nameEmp5", .DischargeDate = New Date(2018, 4, 4)}
        Dim e6 As New employee With {.email = "[email protected]", .name = "nameEmp6", .DischargeDate = New Date(2017, 3, 26)}
        e = New List(Of employee) From {{e4}, {e5}, {e6}}
        Comps.Add(New company With {.name = "nameCom2", .emp = e})



        Dim e7 As New employee With {.email = "[email protected]", .name = "nameEmp7", .DischargeDate = New Date(2019, 1, 8)}
        Dim e8 As New employee With {.email = "[email protected]", .name = "nameEmp8", .DischargeDate = New Date(2018, 6, 30)}
        Dim e9 As New employee With {.email = "[email protected]", .name = "nameEmp9", .DischargeDate = New Date(2017, 2, 26)}
        e = New List(Of employee) From {{e7}, {e8}, {e9}}
        Comps.Add(New company With {.name = "nameCom3", .emp = e})

我会感谢任何建议或评论

vb.net linq lambda linq-to-objects
2个回答
1
投票

第一...

    Dim allEmp = (From co In Comps
                  From emplo In co.emp
                  Select emplo).ToList

    For Each emp As employee In allEmp
        Debug.Print(emp.name)
    Next

第二个...

    Dim dates = (From c In Comps
                 From em In c.emp
                 Where em.DischargeDate > New Date(2018, 1, 1)
                 Select em.DischargeDate)
    Dim minDate = dates.Min
    Debug.Print(minDate.ToString)

1
投票

这样做:

Dim employees = Comps.SelectMany(Function(c) c.emp).ToList()
Dim oldestDischargedEmp As Employee = employees.
          Where(Function(e) e.DischargeDate > #2018-01-01#).
          OrderBy(Function(e) e.DischargeDate).
          First()

要记住的主要事情是你可以使用第一部分的结果来帮助你找到第二部分的答案,从而避免重复一些工作。

还可以考虑删除.ToList()电话。你可以比你可能意识到的更频繁地使用IEnumerable<T>,这样做可以真正有助于提高性能。

© www.soinside.com 2019 - 2024. All rights reserved.