EDIT: If you're only using a List<>
and you only need the index, then List.FindIndex
is indeed the best approach. I'll leave this answer here for those who need anything different (e.g. on top of any IEnumerable<>
).
Use the overload of Select
which takes an index in the predicate, so you transform your list into an (index, value) pair:
var pair = myList.Select((Value, Index) => new { Value, Index })
.Single(p => p.Value.Prop == oProp);
Then:
Console.WriteLine("Index:{0}; Value: {1}", pair.Index, pair.Value);
Or if you only want the index and you're using this in multiple places, you could easily write your own extension method which was like Where
, but instead of returning the original items, it returned the indexes of those items which matched the predicate.