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.
Using Enumerable.SequenceEqual method in LINQ can check whether two arrays are equal. This method is available with . NET framework 3.5 or higher. It works by comparing corresponding elements of both sequences using the custom or default equality comparer.
Compare Arrays in C# Using == (Equality Operator) This method is going to receive the two arrays we want to compare as parameters. After checking if they are equal using == , it is going to return a bool indicating the result of this operation.
AsEnumerable(DataTable) Method (System. Data) Returns an IEnumerable<T> object, where the generic parameter T is DataRow. This object can be used in a LINQ expression or method query.
string[] a = { "a", "b" }; string[] b = { "a", "b" }; return (a.Length == b.Length && a.Intersect(b).Count() == a.Length);
After some performance testing:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With