Updated April 6, 2023
Introduction to LINQ Except
LINQ Except comes under the category of Set Operator in LINQ. The Except() method compares with two collection to return the new list of collection from the first list which not present in the second collection of list. This LINQ Except() method used to return the elements which are present only in the first list items but not in the second list items.
Syntax:
Let’s see the syntax for LINQ Except method as follows,
Var testResult=a1.Except (a2);
In this syntax, we using except method to comparing two lists to retrieve elements from a1 which not present in a2 list.
How except works in LINQ?
Mostly set operator works on two collections for comparison to retrieve the common element, missing element, and unique element, likewise, the LINQ Except used to compare two collections to retrieving elements from first collection which not present in the second collection.
For Example,
Collection X= {2, 4, 6, 8, 10}
Collection Y= {2, 4}
X Expect Y results in = {6, 8, 10}
In this resultant collection, elements 2 and 5 was only available in first collection which is not present in second collection.
using System;
using System. Linq;
using System.Collections.Generic;
class LinqExcept_class
{
//main-method starts here
static public void Main()
{
string[] Courses_1 = { "JAVA", "DOTNET", "PHYTHON", "ANDROID" };
string[] Courses_2 = { "JAVA", "NETWORKING", "ANDROID", "DESIGNING" };
var onlyCourse_1 = Courses_1.Except(Courses_2);
Console.WriteLine("\nUsing Except() Method - Only Listing Course - 1\n");
foreach (var items in onlyCourse_1)
{
Console.WriteLine(items);
}
Console.ReadLine();
}
}
In the above coding there are two string collections Courses_1 and Courses_2 which contains the name of the courses as follows,
string[] Courses_1 = { "JAVA", "DOTNET", "PHYTHON", "ANDROID" };
string[] Courses_2 = { "JAVA", "NETWORKING", "ANDROID", "DESIGNING" };
From this we have to return only first collection elements which not present in the second list collection. For that we need to use the Except() method,
var onlyCourse_1 = Courses_1.Except(Courses_2);
It returns only the first collection elements, which not present in the second collection, let’s check the output as follows,
Output:
Examples
Use the Except() method to get the result as difference it returns the first list values except the values in the second list. To understand clearly the LINQ except method subtracts the elements from the collection, for example, it removes the element of first list items which present in the second list item, and finally, that will be displayed in the resultant set.
Example #1
Code:
using System;
using System.Linq;
using System.Collections.Generic;
// Employee Basic Details
public class EmployeeClass
{
public int employee_id
{
get;
set;
}
public string employee_name
{
get;
set;
}
}
class LinqExcept_class
{
// in main-method
static public void Main()
{
List<EmployeeClass> employeeList_1 = new List<EmployeeClass>() {
new EmployeeClass() {employee_id = 1001, employee_name = "Sasha"},
new EmployeeClass() {employee_id = 1002, employee_name = "Sonali"},
new EmployeeClass() {employee_id = 1003, employee_name = "Vishal"},
new EmployeeClass() {employee_id = 1004, employee_name = "Chintu"},
new EmployeeClass() {employee_id = 1005, employee_name = "Ankath"},
new EmployeeClass() {employee_id = 1006, employee_name = "Saniya"},
};
List<EmployeeClass> employeeList_2 = new List<EmployeeClass>()
{
new EmployeeClass() {employee_id = 1003, employee_name = "Vishal"},
new EmployeeClass() {employee_id = 1004, employee_name = "Chintu"},
new EmployeeClass() {employee_id = 1006, employee_name = "Saniya"},
};
// to find the difference from two list of collections - using Except_Method
Console.WriteLine("LINQ-EXCEPT METHOD\n");
var except_MethodSyntax = employeeList_1.Select(x => x.employee_name).Except(employeeList_2.Select(y => y.employee_name)).ToList();
Console.WriteLine("Employee Names differences from two List using Except Method: \n");
foreach (var eName in except_MethodSyntax)
{
Console.WriteLine(eName);
}
Console.ReadKey();
}
}
Output:
LINQ Except() method using IEqualityComparer
In except method it does not return exact result when using complex types, for that we need to implement IEqualityComparer interface to get exact result from except method. This IEqualityComparer it has two methods GetHashCode and Equals; we need to build new class for this IEqualityComparer interface.
Let’s see the implementation of IEqualityComparer interface for Employee Class as follows,
internal class EmployeeNameComparer : IEqualityComparer<EmployeeClass>
{
public bool Equals(EmployeeClass x, EmployeeClass y)
{
if (string.Equals(x.employee_name, y.employee_name, StringComparison.OrdinalIgnoreCase))
{
return true;
}
return false;
}
public int GetHashCode(EmployeeClass obj)
{
return obj.employee_name.GetHashCode();
}
}
Once creating an instance of EmployeeComparer class then pass that EmployeeComparer instance to the Except() method. Finally, when you passing the EmployeeComparer class in the Except() method you will get the correct exact result as shown below,
Example #2
Code:
using System;
using System. Linq;
using System.Collections.Generic;
// Employee Basic Details
public class EmployeeClass
{
public int employee_id
{
get;
set;
}
public string employee_name
{
get;
set;
}
}
internal class EmployeeComparer : IEqualityComparer<EmployeeClass>
{
public bool Equals(EmployeeClass x, EmployeeClass y)
{
if (string.Equals(x.employee_name, y.employee_name, StringComparison.OrdinalIgnoreCase))
{
return true;
}
return false;
}
public int GetHashCode(EmployeeClass obj)
{
return obj.employee_name.GetHashCode();
}
}
class LinqExcept_class
{
// in main-method
static public void Main()
{
List<EmployeeClass> employeeList_1 = new List<EmployeeClass>() {
new EmployeeClass() {employee_id = 1001, employee_name = "Smith"},
new EmployeeClass() {employee_id = 1002, employee_name = "Peter"},
new EmployeeClass() {employee_id = 1003, employee_name = "Vishal"},
new EmployeeClass() {employee_id = 1004, employee_name = "Chintu"},
new EmployeeClass() {employee_id = 1005, employee_name = "Ankath"},
new EmployeeClass() {employee_id = 1006, employee_name = "Saniya"},
};
List<EmployeeClass> employeeList_2 = new List<EmployeeClass>()
{
new EmployeeClass() {employee_id = 1003, employee_name = "Vishal"},
new EmployeeClass() {employee_id = 1004, employee_name = "Chintu"},
new EmployeeClass() {employee_id = 1006, employee_name = "Saniya"},
};
// to find the difference from two list of collections - using Except_Method - IEqualityComparer
Console.WriteLine("\nLINQ-EXCEPT METHOD USING IEqualityComparer\n\n");
var _newList = employeeList_1.Except(employeeList_2, new EmployeeComparer());
Console.WriteLine("Listing Employee List-1 Details:");
foreach (var item in _newList)
{
Console.WriteLine(item.employee_name);
}
Console.ReadKey();
}
}
Check the below output once passing the EmployeeComparer class in the Except() method you will get exact result as shown below,
Output:
The LINQ Except() method compares with two collection to return the new list of collection from the first list which not present in the second collection of list.
Conclusion
In this article I have explained the LINQ Except concept with several types of examples, hope the article helps to understand the working flow of except method how is comparing the two collections and retrieving the result.
Recommended Articles
This is a guide to LINQ Except. Here we discuss the Introduction, syntax, How except works in LINQ along with examples and code implementation. You may also have a look at the following articles to learn more –