我有这段代码:
int index = Convert.ToInt32(PointSeries.XValues.FirstOrDefault(e => this.PointsRects.Any(ep => ep.BottomLeft.X == e)));
这会返回PointSeries.XValues
列表中x值与PointsRects
列表左下角匹配的项目。我想要PointSeries.XValues
列表中的项目索引(类型为List<double>
)。
答案 0 :(得分:1)
由于你有List<T>
,你可以将LINQ与专门为此目的提供的具体FindIndex
方法混合使用:
int index = PointSeries.XValues.FindIndex(
e => this.PointsRects.Any(ep => ep.BottomLeft.X == e));
返回的值是
第一次出现的与匹配定义的条件匹配的元素的从零开始的索引(如果找到);否则,-1。
如果PointRects
列表很大,您可以通过构建HashSet<double>
并将Any
替换为HashSet.Contains
来进一步优化它:
int index = PointSeries.XValues.FindIndex(
new HashSet<double>(this.PointsRects.Select(ep => ep.BottomLeft.X)).Contains);