Link to home
Start Free TrialLog in
Avatar of Waterside
Waterside

asked on

Linq .Where in VB.net

How do I use Linq to search for an object in a list control.

The list holds objects that have tKey and tValue properties so I'd like to write something like..

myListControl.SelectedItem = myListControl.Items.Where(Item.tValue.Equals("FindMe"))

Thanks
Avatar of Ioannis Paraskevopoulos
Ioannis Paraskevopoulos
Flag of Greece image

Hi,

Try the following:

myListControl.SelectedItem = myListControl.Items.Where(Function (Item) Item.tValue.Equals("FindMe")) 

Open in new window


Giannis
Avatar of Waterside
Waterside

ASKER

I thought that was the way to go myself but got..

Requested operation is not available because the runtime library function 'Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet' is not defined.

..at Item.tValue
For asome crazy reason, you need to Cast it to Object:

	myListControl.SelectedItem = myListControl.Items.Cast(Of Object).Where(Function(x) x.tValuex.tValue.Equals("FindYou")).FirstOrDefault

Open in new window


Giannis
Hi Waterside;

What is the Type of your "list control"?
 
You state the following, "The list holds objects that have tKey and tValue properties", so are you using a KeyValuePair Type? If so what is the DataType of TKey and TValue of the  KeyValuePair(Of Tkey, TValue)?

Can you post the actual line of code and the code surrounding it so we can try to make some sense for the error.
The control is a ListPicker.

The ItemsSource is List(of KeyValuePair(of Integer, String)

I need to set the SelectedItem KVP object for a given key or value.
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that's got it :)
Excellent, glad it worked out for you.