這裏有三種方法來做到這一點,把它放在一個控制檯程序來測試它。一個爲每個循環使用一個,一個使用for循環索引,一個使用linq查詢。我創建了數組,並在其中放入了一些日期,然後將這些日期與DateTime.Now()進行了比較。
Dim dateList As DateTime() = {"1/1/2016", "1/15/2016", "1/31/2016", "2/15/2016"}
' For each loop
For Each d As DateTime In dateList
If d > DateTime.Now Then
Console.WriteLine(d.ToString())
End If
Next
' Loop by index
For x As Integer = 0 To dateList.Count - 1
If dateList(x) > DateTime.Now Then
Console.WriteLine(dateList(x).ToString())
End If
Next
' Linq Query
Dim results = From d In dateList Where d > DateTime.Now()
For Each d As DateTime In results
Console.WriteLine(d.ToString())
Next
Console.ReadKey()