我有一个浮点列表如下:
wayPoints = (new PointF[] {
new PointF(18, 0),
new PointF(18,0),
new PointF(55, 155),
new PointF(55, 155),
new PointF(55, 155),
new PointF(55, 155),
new PointF(55, 0),
new PointF(55, 230)}).ToList();
我想使用Lambda表达式,其中相同的点从列表中删除,但其中一个点保留在列表中
输出:
wayPoints =
(new PointF[] {
new PointF(18, 0),
new PointF(55, 155),
new PointF(55, 0),
new PointF(55, 230)}
).ToList();
我应该如何在Lambda表达式中编写命令?
答案 0 :(得分:2)
Distinct删除了重复项:
var distinctPoints = wayPoints.Distinct();
但是,结果将只包含一个PointF(18,0),即使是像{(18,0),(10,10),(18,0)}这样的序列。我不确定你是否想保留那些不连续的点。
答案 1 :(得分:1)
如果只是一个点列表,那么你可以做到这一点(不需要先做一个数组和ToList()
):
var wayPoints = new List<PointF>{
new PointF(18, 0),
new PointF(18,0),
new PointF(55, 155),
new PointF(55, 155),
new PointF(55, 155),
new PointF(55, 155),
new PointF(55, 0),
new PointF(55, 230)
};
如果你想要独特的点数。您可以使用Distinct
。像这样:
var wayPoints = (new PointF[] {
new PointF(18, 0),
new PointF(18,0),
new PointF(55, 155),
new PointF(55, 155),
new PointF(55, 155),
new PointF(55, 155),
new PointF(55, 0),
new PointF(55, 230)}).ToList();
var uniquePoints=wayPoints.Distinct().ToList();