Reputation: 2140
I have one list with points. I am applying:
var result = neighbors.SelectMany(element => GenerateNeighbors(element)).Distinct<Point3D>().ToList<Point3D>();
To generate all neighbors from each point of this list. But I have another list with other points, and this neighbor-generation can generate points already add to that one.
How to I remove from result list the repeated elements from the other list? Any linq function with some predicate I can use to improve this?
Thanks a lot!
EDIT New code:
class Point3D : IEquatable<Point3D>
{
public int X { get; set; }
public int Y { get; set; }
public int Z { get; set; }
public bool visited { get; set; }
public int life { get; set; }
public Point3D(int _x, int _y, int _z)
{
this.X = _x;
this.Y = _y;
this.Z = _z;
this.visited = false;
this.life = 2;
}
public int GetHashCode(Point3D obj)
{
throw new NotImplementedException();
}
public override Boolean Equals(object o)
{
if (!(o is Point3D)) return false;
return Equals((Point3D)o);
}
public Boolean Equals(Point3D p)
{
return Equals(this, p);
}
public static Boolean Equals(Point3D a, Point3D b)
{
return a.X == b.X && a.Y == b.Y && a.Z == b.Z;
}
}
Upvotes: 0
Views: 822
Reputation: 160852
You can use the Linq Except()
extension method:
result = result.Except(otherList).ToList();
Your Point3D
class should provide an appropriate implementation of Equals
/ GetHashCode
(yes, you do have to implement it, throwing a NotImplementedException
won't work) or implement (more explicit) IEquatable<Point3D>
for this to work, or alternatively you can pass a custom IEqualityComparer
as second parameter to Except()
Upvotes: 5