Pierre Lebon Pierre Lebon - 4 months ago 12
Vb.net Question

Statement in linq lambda expression

I have a list of it, that is my SelectedValue from some ComboBox.

Dim AppsInt = MyApps.CheckedItems.Select(Function(x) Convert.ToInt32(x.Value)).ToList()

And i have this object that is a list( of t)

Dim myObj = New List( Of Item )
Dim FooItem = New item ( 42 )

What I want is to get my list of Int into my object. With Something that would look like this in C#:

AppsInt.foreach( x => myObj .add(new Item(x) ) ) ;

What i have done so far is sending me a "do not produce a result" error:

AppsInt.ForEach( Function(it) myObj.Add(New Item(it)) )

How can i do it ? How to make this linq lambda work?


Lambda expression inside you ForEach expression does not returns any result (and compiler said it to you). It means that you have two ways to solve it:

  1. Add return statement into your lamda expression that will return anything:

               myObj.Add(New Item(it))
               Return 1 ' it's not matter what you will return here.
           End Function)
  2. Change Function(it) to Sub(it). Sub is not obliged to return any value.

Second option is more preferable.