Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulating a 'Where' clause in a For Each loop in VB.NET

Tags:

vb.net

Is it possible to have something similar to an SQL 'WHERE' clause in a For Each loop in VB.NET? I.E.

 FOR EACH event IN events 
    'WHERE eventdate=getdate
 NEXT
like image 548
j82374823749 Avatar asked Dec 29 '22 17:12

j82374823749


2 Answers

in LINQ it would be (.NET 3.5 or higher)

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

And Non-Linq (.Net 2.0 or lower)

For Each event in events
   If event.eventdate = getdate Then
      'Process event
   End If
Next
like image 97
Paully Avatar answered Jan 12 '23 12:01

Paully


With 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
like image 38
Stefan Avatar answered Jan 12 '23 12:01

Stefan