C#中:你如何检查表是相同的尺寸和放大器;相同的元素? [英] C#: how do you check lists are the same size & same elements?

查看:88
本文介绍了C#中:你如何检查表是相同的尺寸和放大器;相同的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有串的两个列表

List<string> A;
List<string> B;

什么是最短code,你会建议检查A.Count == B.Count和A在B和反之亦然每一个元素:每一个B是A(A项和B项可能有不同订购)。

What is the shortest code you would suggest to check that A.Count == B.Count and each element of A in B and vise versa: every B is in A (A items and B items may have different order).

推荐答案

如果你不需要担心重复:

If you don't need to worry about duplicates:

bool equal = new HashSet<string>(A).SetEquals(B);

如果您担心重复,即变得稍微别扭。这将工作,但它的速度相对较慢:

If you are concerned about duplicates, that becomes slightly more awkward. This will work, but it's relatively slow:

bool equal = A.OrderBy(x => x).SequenceEquals(B.OrderBy(x => x));

当然,你可以先检查次数,这是一个简单的EX pression这两个选项更有效。例如:

Of course you can make both options more efficient by checking the count first, which is a simple expression. For example:

bool equal = (A.Count == B.Count) && new HashSet<string>(A).SetEquals(B);

...但你问最短code:)

... but you asked for the shortest code :)

这篇关于C#中:你如何检查表是相同的尺寸和放大器;相同的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆