1

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..

2 Answers 2

8
LeftSelectedItems.ForEach(Sub(a) lb_left.Items.Remove(a))
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.