Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Linq Except Method
The Except() method in LINQ is used to find the set difference between two collections. It returns elements from the first collection that are not present in the second collection, effectively performing a set subtraction operation.
Syntax
Following is the syntax for the Except() method −
public static IEnumerable<TSource> Except<TSource>(
this IEnumerable<TSource> first,
IEnumerable<TSource> second
)
With custom equality comparer −
public static IEnumerable<TSource> Except<TSource>(
this IEnumerable<TSource> first,
IEnumerable<TSource> second,
IEqualityComparer<TSource> comparer
)
Parameters
first − The source collection from which elements will be returned.
second − The collection whose elements will be excluded from the result.
comparer − Optional. An
IEqualityComparerto compare values.
Return Value
Returns an IEnumerable<T> containing elements from the first collection that are not present in the second collection.
Using Except() with Arrays
Example
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.Except(except);
Console.WriteLine("New List...");
foreach (int a in res)
Console.WriteLine(a);
}
}
The output of the above code is −
Initial List... 5 10 15 20 35 40 New List... 5 10 15 40
Using Except() with Strings
Example
using System;
using System.Linq;
using System.Collections.Generic;
class Program {
static void Main() {
string[] fruits1 = { "apple", "banana", "orange", "grape", "mango" };
string[] fruits2 = { "banana", "grape" };
Console.WriteLine("Original fruits:");
foreach (string fruit in fruits1)
Console.WriteLine(fruit);
IEnumerable<string> result = fruits1.Except(fruits2);
Console.WriteLine("\nFruits after except:");
foreach (string fruit in result)
Console.WriteLine(fruit);
}
}
The output of the above code is −
Original fruits: apple banana orange grape mango Fruits after except: apple orange mango
Using Except() with Custom Objects
Example
using System;
using System.Linq;
using System.Collections.Generic;
class Student {
public int Id { get; set; }
public string Name { get; set; }
public override bool Equals(object obj) {
return obj is Student student && Id == student.Id;
}
public override int GetHashCode() {
return Id.GetHashCode();
}
}
class Program {
static void Main() {
List<Student> allStudents = new List<Student> {
new Student { Id = 1, Name = "Alice" },
new Student { Id = 2, Name = "Bob" },
new Student { Id = 3, Name = "Charlie" },
new Student { Id = 4, Name = "David" }
};
List<Student> excludeStudents = new List<Student> {
new Student { Id = 2, Name = "Bob" },
new Student { Id = 4, Name = "David" }
};
var result = allStudents.Except(excludeStudents);
Console.WriteLine("Remaining students:");
foreach (var student in result) {
Console.WriteLine($"ID: {student.Id}, Name: {student.Name}");
}
}
}
The output of the above code is −
Remaining students: ID: 1, Name: Alice ID: 3, Name: Charlie
Key Features
Set Operation −
Except()performs a set difference operation, removing duplicates automatically.Default Equality − Uses the default equality comparer for the type unless a custom comparer is provided.
Deferred Execution − The method uses deferred execution, meaning results are computed when enumerated.
Order Preservation − The order of elements from the first collection is preserved in the result.
Conclusion
The Except() method in LINQ provides an efficient way to find elements that exist in one collection but not in another. It's particularly useful for filtering data, removing unwanted elements, and performing set difference operations on collections of any type.
