Introduction
The Except method is a LINQ extension method which provides a fast way to use set logic, It removes all the elements from one array found in another array. This reduces the need for complex foreach loops.
Example
will remove elements of the first list that are elements in the second list and that will be returned as a third list. See the code below which explains the work of Except.
Code Example
- int[] list1 = { 5, 3, 9, 7, 5, 9, 3, 7 };
- int[] list2 = { 8, 3, 6, 4, 4, 9, 1, 0 };
- Console.WriteLine("Elements of FirstArray(list1 ):{ 5, 3, 9, 7, 5, 9, 3, 7 }");
- Console.WriteLine("Elements of SecondArray(list2 ):{ 8, 3, 6, 4, 4, 9, 1, 0 }");
- int[] Except = list1 .Except(list2 ).ToArray();
- Console.WriteLine("Except Result");
- foreach (int num in Except)
- {
- Console.WriteLine("{0} ", num);
- }
Result
- 5
- 7
Summary
With this demonstrator, i hope you must get ideas how the Except function is working.
Bhuvanesh MohankumarPosted May 6, 2016, 4:03 PM
Good to read...