This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET 3.5+

LINQ Sequence Equality

The fourteenth part of the LINQ to Objects tutorial describes a single standard query operator provided by Language-Integrated Query (LINQ). The SequenceEqual method compares two enumerable sequences to determine if they are an exact match.

SequenceEqual Operator

Language-Integrated Query (LINQ) provides a number of standard query operators in various categories. In this article we will look at the equality category, which contains a single operator named "SequenceEqual". The SequenceEqual operator allows you to supply two collections that implement the IEnumerable<T> interface and determine whether they are exact duplicates. If all of the items in each collection appear in the other, in the same order, they are considered to be identical.

To show the use of the SequenceEqual extension method we will use four arrays, each containing four strings. The first two arrays are identical. The third array contains a lower case copy of each item from the first arrays. The final list has items that match the lower case array but that are in a different order.

var caps1 = new string[] { "A", "B", "C", "D" };
var caps2 = new string[] { "A", "B", "C", "D" };
var lowerCase = new string[] { "a", "b", "c", "d" };
var unordered = new string[] { "d", "b", "c", "a" };

Comparing Sequences

The SequenceEqual method is very easy to use. The method is used as an extension method to the first sequence and the second set of items is provided using the method's parameter. The result of the operation is a Boolean value that is true if the two lists match and false otherwise.

We can check if the first two arrays are identical using the following statement, which returns true.

bool equal = caps1.SequenceEqual(caps2); // true

The default comparer for the type of items being compared is used for each item pair in the two collections. In the case of strings, this is a case-sensitive comparer. When the capitalised and lower case items are tested for equality, the result is false.

bool equal = caps1.SequenceEqual(lowerCase); // false

Using Alternative Comparers

As with many of the other standard query operators that we have seen in the tutorial, the SequenceEqual method can be used with an alternative comparer. This is provided as the second argument to an overloaded version of the method. The comparer will be used for each pair of items in the two arrays.

The following sample shows the upper and lower case arrays being compared using a case-insensitive string comparer. The result of the operation is true.

bool equal = caps1.SequenceEqual(lowerCase, StringComparer.OrdinalIgnoreCase);

// true

Comparing Unordered Data

The order of items in the two collections is critical when checking their equality. If the same data exists in both sets but in a different order, the result of the comparison will be false. This can be seen when comparing the ordered and unordered lower case arrays:

bool equal = lowerCase.SequenceEqual(unordered); // false

There is no overload of SequenceEqual that specifies that the order of items should be ignored. However, if the items in the collections support sorting in a manner that would ensure the same order in both lists, you can order them prior to comparison. This is demonstrated in the final example:

bool equal = lowerCase.OrderBy(s => s).SequenceEqual(unordered.OrderBy(s => s));

// true
6 October 2010