C# Linq Except Method


Get the difference between two arrays using the Except() method.

The following are the two arrays.

int[] arr = { 9, 12, 15, 20, 35, 40, 55, 67, 88, 92 };
int[] arr2 = { 20, 35 };

To get the difference, use Except() method that returns the first list, except the elements in the second list.

arr.AsQueryable().Except(arr2);

The following is the entire example.

Example

 Live Demo

using System;
using System.Linq;
using System.Collections.Generic;
class Program {
   static void Main() {
      int[] arr = { 5, 10, 15, 20, 35, 40 };
      int[] except = { 20, 35 };
      Console.WriteLine("Initial List...");
      foreach(int ele in arr)
      Console.WriteLine(ele);
      IEnumerable<int> res = arr.AsQueryable().Except(except);
      Console.WriteLine("New List...");
      foreach (int a in res)
      Console.WriteLine(a);
   }
}

Output

Initial List...
5
10
15
20
35
40
New List...
5
10
15
40

Updated on: 23-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements