Sandeep Thomas
Sandeep Thomas

Reputation: 4769

Lambda expression error in vb.net

I'm a newbie to this linq stuff. I never used any linq before. So when I had a scenario to move selected items from left list to the right list, I've got a nice solution from search which is in C#, but I converted it into VB. here is the code I've got

Dim leftItems = lb_left.Items.Cast(Of ListItem)().ToList()

Dim rightItems = lb_right.Items.Cast(Of ListItem)().ToList()

'Get all selected items from left box

Dim LeftSelectedItems = leftItems.Where(Function(a) a.Selected).ToList()

'Add all selected items to right box
 'Clear lb_right Items and add sorted list

lb_right.Items.Clear()

LeftSelectedItems.Union(rightItems).OrderBy(Function(a) a.Text).ToList().ForEach(Function(b) lb_right.Items.Add(b))

'Remove all selected items from left box

LeftSelectedItems.ForEach(Function(a) lb_left.Items.Remove(a))

The above is the code I got from internet to move left to right list box. But on that function in the ForEach it is giving me a kinda error "Expression does not produce a value"

I really got stucked with this error. Requesting your fast reply..

Upvotes: 1

Views: 1968

Answers (2)

MarcinJuraszek
MarcinJuraszek

Reputation: 125650

LeftSelectedItems.ForEach(Sub(a) lb_left.Items.Remove(a))

Upvotes: 8

Jon Skeet
Jon Skeet

Reputation: 1504162

From the documentation for VB lambda expressions:

The body of a single-line function must be an expression that returns a value, not a statement. There is no Return statement for single-line functions. The value returned by the single-line function is the value of the expression in the body of the function.

As the compiler says, Add doesn't return a value.

I believe you could use Sub instead of Function, and use the multiline version - but I don't think that's the best way of working here.

It looks like you should be creating a query and then using a sort of "add all these items" call. Unfortunately you haven't told us the type of lb_right, or even whether you're using WPF, WinForms, ASP.NET etc.

Upvotes: 1

Related Questions