LINQ: Compare two Sequences
Posted by Viral Sarvaiya on April 4, 2012
Today i get very good and very simple way to compare two arrays.
.Net provide very simple way to compare sequences with Enumerable.SequenceEqual()
Function SequenceEqual() compare target and source sequence and return Boolean value, so we can know that sequence is similar or not.
for ex.
var ar1 = new[] {1, 2, 3, 4, 5, 6, 7 };
var ar2 = new[] {1, 3, 4, 6 };
var result = ar1.SequenceEqual(ar2);
Console.WriteLine(Result.ToString());
Result : False
Now we want to get that element that is different, for that LINQ gives us best function Except() which works as like below.
if(!result)
{
var differ = ar1.Except(ar2);
Array.ForEach(differ.ToArray(), a => Console.WriteLine(a));
}
Out Put:
2
5
7
Hope this will helps you,
Thanks.



Ajay said
Good one ..
Hi Viral.
Have you any Idea to compare Property Name of two class whether it is same or not
Viral Sarvaiya said
Hi Ajay,
Please check this link
http://stackoverflow.com/questions/506096/comparing-object-properties-in-c-sharp
Hope this will helps you.
Ramani Sandeep said
Good !!!
Minoy Kansara said
That was good example. Thank you for sharing this article.