【发布时间】:2012-12-07 22:44:16
【问题描述】:
我有一个List<CustomPoint> points;,其中包含近百万个对象。
从这个列表中,我想获得恰好出现两次的对象列表。最快的方法是什么?我也会对非 Linq 选项感兴趣,因为我可能也必须在 C++ 中执行此操作。
public class CustomPoint
{
public double X { get; set; }
public double Y { get; set; }
public CustomPoint(double x, double y)
{
this.X = x;
this.Y = y;
}
}
public class PointComparer : IEqualityComparer<CustomPoint>
{
public bool Equals(CustomPoint x, CustomPoint y)
{
return ((x.X == y.X) && (y.Y == x.Y));
}
public int GetHashCode(CustomPoint obj)
{
int hash = 0;
hash ^= obj.X.GetHashCode();
hash ^= obj.Y.GetHashCode();
return hash;
}
}
基于this的回答,我试过了,
list.GroupBy(x => x).Where(x => x.Count() = 2).Select(x => x.Key).ToList();
但这在新列表中给出了零个对象。 有人可以指导我吗?
【问题讨论】:
-
你从哪里得到数据?
-
@MichaelPerrenoud - 这些是地理纬度和经度值
-
是的,但是从哪里加载数据(例如 SQL Server 数据库)?我问是因为您提到它接近一百万个对象,而且您可能需要在 C++ 中访问它
-
@MichaelPerrenoud - 好的..我正在从 shapefile (en.wikipedia.org/wiki/Shapefile) 中读取这些点。我正在使用一个名为 Quantum GIS 的应用程序及其 C++ API..
-
你可能知道也可能不知道,但是比较双精度数是否相等通常不是一个好主意。见stackoverflow.com/questions/1398753/…
标签: c# duplicates