I have this list of cars
ICollection<Cars> Cars
and I also have a single car object, which I know is in the ICollection how do I get the index/position of the car in the list? I need to add it to a list of strings
This is what I have so far:
var index = cars.Where(b=> b.Id == car.id).Single().iWantTheIndex
stringList.Add(index)
Any ideas?
FindIndex()
method. – Demarchevar index = cars.Select((b, i) => new { car = b, index = i }).Single(b => b.car.Id == car.id).index;
– RadfordICollection<T>
even have indexes? How do you get an element fromCars
based on its index? – DevoutICollection
's don't necessarily have indexes. – Frenumcars
is. The system provides no guarantee that the same "index" would match the same object in any future operation. One could, on the very next line of code, attempt to fetch the same object fromcars
based on that index (likely using.Skip()
and.First()
or something of that nature) and get a completely different object. – DevoutICollection<T>
is the wrong type to use. The OP is looking forIList<T>
. – DevoutIList<T>
) rather than one that doesn't (ICollection<T>
). It's a very bad idea to use an interface whose contract doesn't actually specify all of the things you need, and to just hope that anyone using that code knows that they're actually supposed to provide specific types of implementations. – Frenum