我有一个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;
}
}
Run Code Online (Sandbox Code Playgroud)
基于这个答案,我试过,
list.GroupBy(x => x).Where(x => x.Count() = 2).Select(x => x.Key).ToList();
Run Code Online (Sandbox Code Playgroud)
但是这会在新列表中给出零对象.有人可以指导我吗?