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

how can i get a collection from an item in another collection that is also a collection

我需要:

  1. a list(of employee)包含所有公司包含的所有员工。 (e1,e2...e9)

  2. 员工最早的 DischargeDate 是如何高于 1/1/2018

我已经通过这种方式解决了,但我认为可以通过更清晰,更高效的方式实现。 也许使用 Linq or/and 一些 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 = "aaa@aaa.aaa", .name = "nameEmp1", .DischargeDate = New Date(2019, 1, 12)}
        Dim e2 As New employee With {.email = "bbb@bbb.bbb", .name = "nameEmp2", .DischargeDate = New Date(2018, 8, 24)}
        Dim e3 As New employee With {.email = "ccc@ccc.ccc", .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 = "ddd@ddd.ddd", .name = "nameEmp4", .DischargeDate = New Date(2014, 3, 22)}
        Dim e5 As New employee With {.email = "eee@eee.eee", .name = "nameEmp5", .DischargeDate = New Date(2018, 4, 4)}
        Dim e6 As New employee With {.email = "fff@fff.fff", .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 = "ggg@ggg.ggg", .name = "nameEmp7", .DischargeDate = New Date(2019, 1, 8)}
        Dim e8 As New employee With {.email = "hhh@hhh.hhh", .name = "nameEmp8", .DischargeDate = New Date(2018, 6, 30)}
        Dim e9 As New employee With {.email = "iii@iii.iii", .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})

我将不胜感激任何建议或评论

第一个...

    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)

这两个:

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> 的次数可能比您想象的要多得多,这样做确实有助于提高性能。