Jason D
Jason D

Reputation: 2664

Find index in list of custom objects

I have a list: List<myObject> myList. It currently holds 3 myObjects. Given an int, I need to know how to find the index of any one of those objects so that I can use it elsewhere. I have tried using lambda statements but for some reason they absolutely do not want to work. I'm actually unsure if they're appropriate for this scenario. I have tried using myList's IndexOf, but I can't seem to get that to work, probably because I'm using custom objects.

If anybody can tell me how to accomplish this I would really appreciate it.

Upvotes: 0

Views: 93

Answers (3)

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48558

how about

var myobject = myList.ToArray()[myInt];

or just

var myobject = myList[myInt];

Upvotes: 2

jamesthollowell
jamesthollowell

Reputation: 1712

If you can give us some code that would help.

Without any more info, here is something you can try.

int indexFound = Array.IndexOf<myObject>(myList.ToArray(), objectToLocateIdexOf);

Upvotes: 0

Sachin
Sachin

Reputation: 40970

Try simply this

var myobject = myList[value];

Upvotes: 2

Related Questions