LINQ is a powerful feature in C# that allows developers to query and manipulate data from various sources using a uniform syntax. One of the useful methods provided by LINQ is the Except method, which allows you to find the set difference between two sequences. In this article, we will explore the Except method and provide examples to help you understand its usage.

 
Understanding the LINQ Except Method

The LINQ Except Method is used to return the elements which are present in the first data source but not in the second data source. It is a LINQ query method that takes two collections as input and returns a new collection that contains the elements of the first collection that are not present in the second collection. The syntax for using the Except method is as follows:

 
What happens if any of the sequences is null?

The Except Method will throw an exception if any of the sequences is null. In the below example, the second sequence is null and while performing the Except operation using the Except Method it will throw an exception.

Now run the application and you will get the following exception: System.ArgumentNullException: 'Value cannot be null. (Parameter 'second')'

 
Understand LINQ Except Method with String Data Type

In the below example, I am showing how to use LINQ Except Method with String Data using both Method and Query Syntax. Here, we have a string array of countries and we need to return the countries from the first collection, that are not present in the second collection using Except Method. So, modify the Main Method of the Program class as follows.

In spite of having the country UK in the second collection, it still shows in the output. This is because the default comparer that is being used to filter the data by the Except Method is case-insensitive. So if you want to ignore the case-sensitive, then you need to use the other overloaded version of the Except() method which takes IEqualityComparer as an argument. So, modify the Main Method of the Program as shown below. Here, we are passing StringComparer as an argument and this StringComparer class already implements the IEqualityComparer interface.

 

LINQ Except() Method with Complex Type in C#:

The LINQ Except() Method in C# works slightly different manner when working with complex types such as Employee, Product, Student, etc. Let us understand this with an example. Create a class file with the name Student.cs and then copy and paste the following code into it.

This is a very simple student class with just two properties. Let’s say, we have the following two data sources.

As you can, we have two data sources. The first data source i.e. AllStudents holds the information of all the students while the second data source i.e. Class6Students holds the data of only the 6th-class students. Now, our requirement is only to fetch all the student names except the 6th-class students. That means we need to fetch the student names from the AllStudents data source which are not present in the second data source i.e. Class6Students.

Now we need to select all the information of all the students from the first data source which is not present in the second data source. Let us modify the Main Method of the program class as shown below.

 
Using Anonymous Type with Except Method in C#:

In this approach, we need to select all the individual properties to an anonymous type. The following program does exactly the same thing.

 
Using IEqualityComparer Comparer with LINQ Except Method in C#:

In this approach, we need to create a class and then we need to implement the IEqualityComparer interface and we need to implement the Equals and GetHashCode method. So, create a class file with the name StudentComparer.cs and then copy and paste the following code into it.

Next, we need to create an instance of StudentComparer class and need to pass that instance to the Except method. So, modify the Main method of the Program class as follows.

 
Overriding Equals() and GetHashCode() Methods within the Student Class

In C#.NET, by default, any type (predefined or user-defined) is inherited from the Object class. That means the Student class is also inherited from the Object class. And, we also know that the Object class provides some virtual methods such as Equals() and GetHashCode(). So, now, we need to override the Equals() and GetHashCode() methods of the Object class within the Student class. So, modify the Student class as shown below. Here, we are overriding the Equals() and GetHashCode() methods.

With the above changes in place, now modify the Main method of the Program class as shown below. Now, we need to use the overloaded version of the Except method which does not take any parameter.

 

Now execute the above program and it will display the output as expected as shown in the below image.

Implementing IEquatble<T> Interface in Student Class.

In this approach, we need to implement the IEquatble<T> Interface in Student Class and we need to implement the Equals Method of the IEquatble<T> Interface and we also need to override the GetHashCode method of the Object class. So, modify the Student class as shown below.

As you can see, here we have done two things. First, we implement the Equals method of the IEquatable interface and then we override the GetHashCode method of the Object class. With the above changes in place, now modify the Main Method of the Program class as shown below.

Run the application and you should see the output as expected as shown in the below image.

In the next article, I am going to discuss the LINQ Intersect Method in C# with Examples. In this article, I try to explain the LINQ Except Method in C# with Examples. I hope you enjoy this article and understand the Concept of LINQ Except Method in C# with Examples.

Leave a Comment