Distinct List Elements

262 views Asked by At

I have a list collection of following type

CoordinateCollection pointCoordinates = new CoordinateCollection();

it adds up list of type vector. I want to remove duplicate entries from the list

I am trying like this

pointCoordinates = pointCoordinates.Distinct();

it gives me an error

cannot implicitly convert type 'system.collections.generic.ienumerable<SharpKML.Base.Vector> to SharpKML.Dom.CoordinateCollection

Please help me in solving this. I want unique set of records

3

There are 3 answers

1
Tim Schmelter On BEST ANSWER

Since CoordinateCollection implements ICollection<Vector> and Vector overrides Equals + GethashCode you can use Distinct. But you need to use the constructor of CoordinateCollection to create a new:

pointCoordinates = new CoordinateCollection(pointCoordinates.Distinct());

Note that i'm not familiar with SharpKML, i have looked at it's source code.

0
Sacrilege On

Distinct is going to return an IEnumerable<SharpKML.Base.Vector>. You can't assign that type to your CoordinateCollection instance because even though is is an IEnumerable<SharpKML.Base.Vector> it may not also be an instance of CoordinateCollection.

If you need an instance of CoordinateCollection create/use a constructor that will take an IEnumerable<SharpKML.Base.Vector> as input otherwise if an IEnumerable<SharpKML.Base.Vector> will do then declare your variable as such.

0
Gurgadurgen On

As the message states, you can't implicitly assign an object of type system.collections.generic.ienumerable<SharpKML.Base.Vector> to an object of type SharpKML.Dom.CoordinateCollection. I would suggest using one of their type conversion methods (documented here).