Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using LINQ's ForEach with anonymous methods in VB.NET

I'm trying to replace the classic For Each loop with the LINQ ForEach extension in VB.NET...

  Dim singles As New List(Of Single)(someSingleList)
  Dim integers As New List(Of Integer)

  For Each singleValue In singles
    integers.Add(CInt(Math.Round(singleValue)))
  Next singleValue

Maybe something like this?

  singles.ForEach(Function(s As [Single]) Do ???

How can I correctly do this using anonymous methods (i.e. without declare a new function)?

like image 364
serhio Avatar asked May 16 '11 10:05

serhio


1 Answers

Try this:

singles.ForEach(Sub(s As [Single]) integers.Add(CInt(Math.Round(s))))

You need a Sub here, because the body of your For Each loop doesn't return a value.

like image 126
Daniel Hilgarth Avatar answered Sep 18 '22 22:09

Daniel Hilgarth