User1551892 User1551892 - 25 days ago 9
C# Question

How to find the indices of items fulfilling some condition in List of int?

I have a list of int containing items { 10, 11, 23, 34, 56, 43 } and I want to find out the indices of all items which are greater than 23. These values can be any order so I do not want to sort them.

List<int> mylist = new List<int> { 10, 11, 23, 34, 56, 43 };


I am interested in indices of all items those are fulfilling the condition and not only first item which fulfill condition. so this line of code is not working for me.

int index = mylist.FindIndex( x => x > 23 );

Answer
var indexes = mylist.Select((v, i) => new { v, i })
                    .Where(x => x.v > 23)
                    .Select(x => x.i);