我有 2 个点列表 ( List<Point> ) 用于某些标签元素的坐标。一个列表用于移动之前,一个列表用于移动之后,因此索引引用相同的标签元素。我想比较具有相同索引的每个元素,看看哪些元素的点发生了变化。
List<int> changedIndexes = new List<int>();
for(int i = 0; i < labelLocationsBefore.Count; i++)
{
if (labelLocationsBefore[i].X != labelLocationsAfter[i].X || labelLocationsBefore[i].Y != labelLocationsAfter[i].Y)
{
changedIndexes.Add(i);
}
}
这就是这个循环所做的。但我如何将其转换为 Linq 表达式并检索更改后的标签索引?
请您参考如下方法:
您正在寻找this overload的 Select采用 Func<T, int, bool> 的方法其中第二个参数是索引:
changedIndexes = labelLocationsBefore
.Select((point,idx) => new { point, idx })
.Where(p => p.point.X != labelLocationsAfter[p.idx].X ||
p.point.Y != labelLocationsAfter[p.idx].Y)
.Select(p => p.idx)
.ToList();
