LINQ: Compare two Sequences

How do you compare two sequences using LINQ? The answer is by using the Enumerable.SequenceEqual(). SequenceEqual() compares the source and target sequences elements, by using the default equality comparer for their type, and returns a Boolean.


There are many ways to use the SequenceEqual() extension method – like to compare the files in two folders and see if they contain the same files. Let us see a simple example of using the SequenceEqual() extension method

LINQ SequenceEqual

The output will be: False

Now what if you want to find out the difference between these two sequences?. Use this piece of code which uses the IEnumerable.Except extension method:

LINQ Except

OUTPUT

LINQ Compare Sequences

1 comment:

  1. Hi, interesting article, but I have a question: what if you do l2.Except(l1) ?

    Thanks

    ReplyDelete