C# 合并两个集合的不同项
我正在寻找一种高性能的方法来将第二个 ICollection 的不同项目添加到现有的 ICollection 中。我正在使用.NET 4。
I'm looking for a performant way to add distinct items of a second ICollection to an existing one. I'm using .NET 4.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这应该可以做到:
This should do it:
只要它们是 IEnumerable,您就可以使用首选 Linq 答案:
这将使用默认的相等比较,对于大多数对象来说是引用相等。要更改此设置,您可以为集合中的项目类型定义一个 IEqualityComparer 泛型,它将执行更多语义比较,并将其指定为 Union 的第二个参数。
As long as they're IEnumerable, you can use the go-to Linq answer:
This will use the default equality comparison, which for most objects is referential equality. To change this, you can define an IEqualityComparer generic to the item type in your collection that will perform a more semantic comparison, and specify it as the second argument of the Union.
添加到现有列表的另一种方法是:
Another way to add to your exisiting list would be:
对你的问题最直接的答案 - 因为你没有给出关于你作为输入或需要作为输出的 ICollection 的实际类型的详细信息,这是 KeithS 给出的,
这将返回一个不同的 IEnumerable - 如果这就是你所需要的它非常快。我制作了一个小型测试应用程序(如下),该应用程序针对简单的重复数据删除哈希集方法运行联合方法(方法A)并返回哈希集<>(方法B)。 union 方法破坏哈希集:
然而——必须将该 IEnumerable 转换为某种其他类型的集合,例如 List<> (就像 AD 发布的版本一样)改变了一切:
只需将 .ToList() 添加到 MethodA 即可
更改结果:
因此,似乎需要更多地了解您正在处理的具体情况,并且您提出的任何解决方案都应该进行测试,因为小的(代码)更改可能会产生巨大的影响。
下面是我用来比较这些方法的测试 - 我确信这是一种愚蠢的测试方法 - 但它似乎有效:)
The most direct answer to your question - since you didn't give much detail on the actual types of ICollection you have as input or need as output is the one given by KeithS
This will return a distinct IEnumerable - if that is what you need then it is VERY fast. I made a small test app (below) that ran the union method (MethodA) against a simple hashset method of deduplicating and returns a Hashset<>(MethodB). The union method DESTROYS the hashset:
However -- Having to convert that IEnumerable to some other type of collection such as List<> (like the version ADas posted) changes everything:
Simply adding .ToList() to MethodA
Changes the results:
So - it seems more would need to be known about the specific case you are working with - and any solution you come up with should be tested - since a small (code) change can have HUGE impacts.
Below is the test I used to compare these methods - I'm sure it is a stupid way to test - but it seems to work :)