我需要:
一个list(of employee)
,其中包含所有公司中包含的所有员工。 (e1,e2 ... e9)
员工的最高离职日期如何高于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 = "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})
我将不胜感激任何建议或评论
答案 0 :(得分:1)
第一个...
<div class="card mx-auto mb-2" style="width: 12rem;">
<div class="d-flex align-items-center" style="height:200px;">
<img class="card-img-top" src="https://www.fillmurray.com/200/100" alt="Card image cap">
</div>
<div class="card-body">
<p class="card-text">Verticall center this image</p>
</div>
</div>
第二个...
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
答案 1 :(得分: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>
,您就能比想像的要频繁得多,这样做确实可以提高性能。