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, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 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

QualityPoint 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 .