Abhinav Pandey
Abhinav Pandey

Reputation: 183

Find index of array with specific length in a List?

In my application, I have a List<float[]> with arrays varying in sizes each time the application runs. I want to find out the index of arrays in that List<float[]> with length less than or equal to a specified value.

Lets say I have a List [ float[100], float[80], float[101] ]. Now I want to know the indexes of those arrays whose length is less than or equal to 100.

I can create Loop and Iterate through each element in the list but that way seems too long. Is there any LINQ way possible?

Upvotes: 0

Views: 628

Answers (1)

Mureinik
Mureinik

Reputation: 312136

You could use Linq's Select to get the indexes, and Where to filter them out:

List<int> indexes = list.Select((arr, ind) => (arr, ind))
                        .Where(x => x.arr.Length <= 100)
                        .Select(x => x.ind)
                        .ToList();

Upvotes: 3

Related Questions