1

I have a list of float points as following:

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();

I want to use Lambda Expressions in which the same points removed from list but one of those same points stays in the list

Output:

wayPoints = 
(new PointF[] { 
 new PointF(18, 0),
 new PointF(55, 155),
 new PointF(55, 0),
 new PointF(55, 230)}
).ToList();

How should I write my commands in Lambda Expressions?

4

2 回答 2

2

Distinct删除重复项:

var distinctPoints = wayPoints.Distinct();

然而,结果将只包含一个 PointF(18, 0),即使是像 {(18, 0), (10, 10), (18, 0)} 这样的序列。我不确定您是否要保留那些不连续的点。

于 2012-04-17T08:15:04.763 回答
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();
于 2012-04-17T08:06:43.757 回答