Mega Code Archive

 
Categories / C# Book / 04 LINQ
 

0399 Except

Except returns the elements in the first input sequence that are not present in the second: using System; using System.Collections; using System.Collections.Generic; using System.Linq; class Program { static void Main() { int[] seq1 = { 1, 2, 3 }; int[] seq2 = { 3, 4, 5 }; IEnumerable<int> difference1 = seq1.Except(seq2); IEnumerable<int> difference2 = seq2.Except(seq1); foreach (int i in difference1) { Console.WriteLine(i); } foreach (int i in difference2) { Console.WriteLine(i); } } } The output: 1 2 4 5