在VB.NET中,可能有與For Each
循環中的SQL'WHERE'子句類似的東西嗎? I.E.模擬VB.NET中的For Each循環中的'Where'子句
FOR EACH event IN events
'WHERE eventdate=getdate
NEXT
在VB.NET中,可能有與For Each
循環中的SQL'WHERE'子句類似的東西嗎? I.E.模擬VB.NET中的For Each循環中的'Where'子句
FOR EACH event IN events
'WHERE eventdate=getdate
NEXT
在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
你不能做你在每個循環中描述的內容。你可以在C#中的for循環中做到這一點,但我不認爲你可以在VB.NET中。
你真的有多個選項可以解決這個問題。
一種方法是做到這一點:
For Each event As Event in events
If eventdate = getdate then
Continue For
End If
Next
您還可以使用LINQ,但它是在這種情況下值得商榷是否會幫助你。
當然是,這個例子應該足以讓你去:
For Each dvr As DataRowView In dv
If dvr("IsVisible") = False Then
Continue For
End If
' Do something here
Next
隨着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
+1,這是我會給出的答案。儘管如此,不需要「選擇人」。 – 2009-08-20 05:35:36
Jakob,我不確定我可以省略最後的「select pers」,並且無法在發佈前對其進行測試,但現在已經編輯了答案。謝謝你的觀點! – Stefan 2009-08-20 13:50:26
我的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
使用LINQ /擴展方法,是的。你使用的是什麼版本的VB/.NET? – 2009-08-20 00:19:33