Sunday 25 August 2013

LINQ - usage of Except method in Linq

In this post let we see the difference between the collections , using Linq. Let we take a two examples first is compare with collection then with collection two.

var seq1 = new List<int>() {1,6,3,21};
var seq2 = new List<int>() {4,6,21,3};


Now let take a example of two collections, following example is used to find the values of collection seq1 which is not present in collection seq2.

/*Difference between two sequence */
   var diff=seq1.Except(seq2);

    foreach (int d in diff)
       Console.WriteLine(d);

Output : 

1

Now let take a example of two collections, following example is used to find the values of collection seq2 which is not present in collection seq1.

/*Difference between two sequence */
   var diff=seq2.Except(seq1);

    foreach (int d in diff)
       Console.WriteLine(d);

Output : 

4



From this article i hope you can understand how to get the difference between the collections.

No comments:

Post a Comment