2009-08-20 73 views

回答

6

在LINQ這將是(.NET 3.5或更高版本)

For Each event in events.Where(Function(x) x.eventdate = getdate) 
     'Process event 
Next 

及非LINQ的(。淨2.0或更低)

For Each event in events 
    If event.eventdate = getdate Then 
     'Process event 
    End If 
Next 
0

你不能做你在每個循環中描述的內容。你可以在C#中的for循環中做到這一點,但我不認爲你可以在VB.NET中。

你真的有多個選項可以解決這個問題。

一種方法是做到這一點:

For Each event As Event in events 
    If eventdate = getdate then 
     Continue For 
    End If 
Next 

您還可以使用LINQ,但它是在這種情況下值得商榷是否會幫助你。

0

當然是,這個例子應該足以讓你去:

For Each dvr As DataRowView In dv 
    If dvr("IsVisible") = False Then 
     Continue For 
    End If 

     ' Do something here 
Next 
2

隨着LINQ:

For Each p As Person In (From pers In Persons Where pers.Firstname = "Stefan") 
      'Only handle persons with first name "Stefan" 
      MsgBox(p.LastName) 
     Next 
    End Sub 
+0

+1,這是我會給出的答案。儘管如此,不需要「選擇人」。 – 2009-08-20 05:35:36

+0

Jakob,我不確定我可以省略最後的「select pers」,並且無法在發佈前對其進行測試,但現在已經編輯了答案。謝謝你的觀點! – Stefan 2009-08-20 13:50:26

1

我的2美分:

如果您使用的列表(Of T)已VB 8.0(.NET Framework 2.0中).FindAll:


Public Shared Sub Show() 
    Dim filtredEvents As List(Of Event) = New List(Of Event)().FindAll(Function (ByVal e As Event) 
     Return (e.EventDate = DateTime.Today) 
    End Function) 
    Dim anEvent As Event 
    For Each anEvent In filtredEvents 
     Console.WriteLine(anEvent.EventDate) 
    Next 
End Sub