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


LINQ Compare Sequences

About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

1 comment:

Fabian Fernandez said...

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