Drag and Drop
Drag and Drop

Reputation: 2734

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?

Upvotes: 1

Views: 60

Answers (2)

Marcel Hoekstra
Marcel Hoekstra

Reputation: 1488

you should change function(it) to sub(it) . Or:

Dim myObj = AppsInt.Select( Function(it) New Item(it)).ToArray()

Upvotes: 2

feeeper
feeeper

Reputation: 3017

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:

    AppsInt.ForEach(Function(it) 
               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.

Upvotes: 1

Related Questions