任何内置方法只知道两个列表是否不同?
我必须比较两个列表,并且仅知道它们是否包含相同的值。我不打算对这些差异做任何事情。
在 C# 中实现这一目标的最佳方法是什么?
我知道我可以循环列表,但想知道是否有任何内置的 LINQ/扩展方法来实现此目的,从而提供更好的性能。尝试过 except/Intersect,但不知道它们是否是实现此目标的最合适的方法。
更新:列表中不会有任何重复项。
I have to compare two lists and only know if they contain same values or not. I do not intend to do anything with the differences.
What would be the best way to achieve this in C#?
I know I can loop the lists but want to know if theres any in-built LINQ/extenstion method to achieve this which provides better performance. Did try Except/Intersect, but don't know if they are the most suitable ones to achieve this.
Update: The lists won't have any duplicates within them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想如何处理重复项?例如,您会将 { 2, 1, 1 } 视为与 { 1, 2 } 相同吗? (我假设顺序无关......否则只需使用
SequenceEqual
。)假设您只有效地关心“集合”,这里有两个选项:
一种快速而肮脏的方法:
一种稍微干净一点的 方法方式:
需要考虑的一件事:如果您只对将它们视为集合感兴趣,那么您可能需要使用集合表示来开始,而不是列表......
How do you want to handle duplicates? For example, would you count { 2, 1, 1 } as being the same as { 1, 2 }? (I'm assuming that the order is irrelevant... otherwise just use
SequenceEqual
.)Assuming you only care about "sets" effectively, here are two options:
A quick and dirty way:
A slightly cleaner way:
One thing to consider: if you're only interested in treating them as sets, you might want to use a set representation to start with, instead of a list...