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
WatersideAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ioannis ParaskevopoulosCommented:
Hi,

Try the following:

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

Open in new window


Giannis
0
WatersideAuthor Commented:
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
0
Ioannis ParaskevopoulosCommented:
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
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Fernando SotoRetiredCommented:
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.
0
WatersideAuthor Commented:
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.
0
Fernando SotoRetiredCommented:
Hi Waterside;

See if this works for you any better.
Dim tValueToFind As String = "Visual Basic"
myListControl.SelectedItem = myListControl.Items.Cast(Of KeyValuePair(Of Integer, String)).
                                   Where(Function(item) item.Value.Equals(tValueToFind)).
                                   FirstOrDefault()

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WatersideAuthor Commented:
that's got it :)
0
Fernando SotoRetiredCommented:
Excellent, glad it worked out for you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.