我正在寻找一个 linq 表达式,它是 FindIndex 的扩展方法。它只返回第一个索引。我想要满足条件的列表中的所有索引。
例如:
var indx = myList.FindIndex(x => (x <= -Math.PI / 3) || (x >= Math.PI / 3));
最佳答案
那么您需要使用 LINQ,因为 List.FindIndex
仅返回第一个。您可以使用 Enumerable.Select
的重载,它给出序列中项目的索引来创建匿名类型。
IEnumerable<int> allIndices = myList
.Select((item, index) => new { item, index })
.Where(x => (x.item <= -Math.PI / 3) || (x.item >= Math.PI / 3))
.Select(x => x.index);
关于c# - 满足给定条件的元素的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19908241/