Why are use the LINQ Except Method, give an example of Except method.

Why are use the LINQ Except Method, give an example of Except method.

Last updated:9/24/2021 2:58:45 AM

1 Answers

Ravi Vishwakarma
Ravi Vishwakarma

In LINQ, the Except method or operator is used to return only these elements from the first collection or list which are not present in the second collection or list. in another word first collection elements are doesn't present in the second collection then return the remaining first collection element. 

Syntax

collection1.Except(collection2)

Example

using System;

using System.Collections.Generic;
using System.Linq;
public class Program
{
 public static void Main()
 {
  List<string> count1= new List<string>() { 'UK', 'Australia', 'India', 'USA' };
  List<string> count2 = new List<string>() { 'India', 'UAE', 'Canada', 'UK', 'China', 'Pok' };
  List<string> list = count1.Except(count2).ToList();
  list.ForEach( country => Console.WriteLine(country) );
  Console.ReadLine();
 }
}

Output

Australia

USA

Answer