在C#中使用LINQ比较数组 [英] Comparing Arrays using LINQ in C#

查看:138
本文介绍了在C#中使用LINQ比较数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组像

 的String [] A = {A,B,C }; 
串[] B = {一,B,C};



我需要使用LINQ两个数组进行比较。



的比较应该只发生如果两个数组具有相同的大小。这些数据可以是任意顺序,仍然若B []的所有值的[]所有的值,并且是同一返回true。


解决方案

 的String [] A = {A,b}; 
串[] B = {一,B};

回报率(则为a.length == b.length个和放大器;&安培; a.Intersect(B).Count之间的()==则为a.length);






在一些性能测试:




  • 超过10,000小弦 - 5毫秒

  • 超过十万小弦 - 99ms

  • 超过一百万的小弦 - 魅力。 601ms

  • 超过十万〜500字符串 - 190ms


I've two arrays like

string[] a = { "a", "b", "c" };
string[] b = { "a", "b", "c" };

I need to compare the two arrays using LINQ.

The comparison should take place only if both arrays have same size. The data can be in any order and still return true if all values of a[] and all values of b[] are the same.

解决方案

string[] a = { "a", "b" };
string[] b = { "a", "b" };

return (a.Length == b.Length && a.Intersect(b).Count() == a.Length);


After some performance testing:

  • Over 10,000 small strings - 5ms
  • Over 100,000 small strings - 99ms
  • Over 1,000,000 small strings - Avg. 601ms
  • Over 100,000 ~500 character strings - 190ms

这篇关于在C#中使用LINQ比较数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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