How do you find the index of an element in an inherited Collection <T> class?
How do you find the index of an element in an inherited Collection class?
public class MyCollection : Collection<MyClass>
{
// implementation here
}
I tried using .FindIndex in the collection, but failed:
int index = collectionInstance.FindIndex(someLambdaExpression);
Any other ways to achieve this?
+3
3 answers
If you have an element directly, you can use IndexOf to get it. However, this does not help to find the index of the element through lambda.
You can use LINQ, however:
var index = collectionInstance.Select( (item, index) => new {Item = item, Index = index}).First(i => i.Item == SomeCondition()).Index;
+6