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

Will you give this article a +1 ? Thanks in advance


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.

2 comments:

Fabian Fernandez said...

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

Thanks

Anonymous said...

yeah a nice blog to read ..even though the contents are less it has got lot of useful information..I feel that you could have added information about .You can test your knowledge in html by attending HTML Quiz .