简体   繁体   English

不同的列表元素

[英]Distinct List Elements

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 我想要一套独特的记录

Since CoordinateCollection implements ICollection<Vector> and Vector overrides Equals + GethashCode you can use Distinct . 由于CoordinateCollection实现ICollection<Vector>Vector覆盖Equals + GethashCode您可以使用Distinct But you need to use the constructor of CoordinateCollection to create a new: 但是您需要使用CoordinateCollection的构造函数来创建一个新的:

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

Note that i'm not familiar with SharpKML , i have looked at it's source code . 请注意,我不熟悉SharpKML ,我查看了它的源代码

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 . 作为消息的状态,则无法隐式类型的对象分配system.collections.generic.ienumerable<SharpKML.Base.Vector>到类型的对象SharpKML.Dom.CoordinateCollection I would suggest using one of their type conversion methods (documented here ). 我建议使用其中一种类型转换方法( 此处记录 )。

Distinct is going to return an IEnumerable<SharpKML.Base.Vector> . Distinct将返回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 . 您不能将该类型分配给您的CoordinateCollection实例,因为即使它是IEnumerable<SharpKML.Base.Vector>它也可能不是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. 如果需要CoordinateCollection的实例,请创建/使用一个构造函数,该构造函数将IEnumerable<SharpKML.Base.Vector>作为输入,否则如果IEnumerable<SharpKML.Base.Vector>将执行,则声明您的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM