2009-08-20 9 views

Répondre

6

dans LINQ il serait (.NET 3.5 ou supérieur)

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

et non-Linq (. net 2.0 ou inférieur)

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

Vous ne pouvez pas faire ce que vous décrivez dans une boucle pour chaque boucle. Vous pouvez le faire dans une boucle for en C#, mais je ne pense pas que vous pouvez en VB .NET.

Vous avez vraiment plusieurs options pour résoudre ce problème.

Une façon serait de le faire:

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

Vous pouvez aussi le faire en utilisant LINQ, mais il peut soutenir dans ce cas, si cela vous aiderait.

0

Bien sûr, il est, cet exemple devrait être suffisant pour vous aller:

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

     ' Do something here 
Next 
2

Avec 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, c'est la réponse que j'aurais donnée. Pas besoin de "Select pers" à la fin, cependant. –

+0

Jakob, je n'étais pas sûr que je pouvais omettre le "select pers" à la fin et je ne pouvais pas le tester avant de poster, mais j'ai édité la réponse maintenant. Merci pour le point! – Stefan

1

Mes 2 cents:

Si vous utilisez VB 8.0 (.NET Framework 2.0) avec List (Of T) .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 

Questions connexes