【问题标题】:how can i get a collection from an item in another collection that is also a collection我如何从另一个集合中的项目中获取集合,该集合也是集合
【发布时间】:2019-04-06 15:12:54
【问题描述】:

我需要:

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

  2. 员工最早的 DischargeDate 如何高于 2018 年 1 月 1 日

我已经通过这种方式解决了,但我认为可以通过更清晰和更有效的方式来实现。 也许使用 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 = "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})

我会很感激任何建议或评论

【问题讨论】:

    标签: vb.net linq lambda linq-to-objects


    【解决方案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)
    

    【讨论】:

    • 两者都工作得很好,谢谢,这正是我想要的。
    • @JCM 请点击我的答案左侧的复选标记(勾号)接受我的答案。
    【解决方案2】:

    这两者兼而有之:

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

    【讨论】:

    • 这也很有效。订购结果以便您可以只拿第一个是一个好主意,我下次会记住它。非常感谢!
    猜你喜欢
    • 2016-05-16
    • 2023-03-21
    • 1970-01-01
    • 2020-02-05
    • 2013-05-28
    • 2021-06-03
    • 2010-10-17
    • 2013-12-21
    • 1970-01-01
    相关资源
    最近更新 更多