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
How to use "not in" query with C# LINQ?
In C# LINQ, there are several ways to perform "not in" queries to exclude items from one collection that exist in another. The most common approaches use the Except operator, the Where clause with Any, or the Contains method.
These operators work with any data source that implements IEnumerable<T>, making them versatile for querying collections, arrays, and LINQ query results.
Syntax
Using the Except operator −
var result = collection1.Except(collection2);
Using Where with Any −
var result = from item in collection1
where !collection2.Any(x => x == item)
select item;
Using Where with Contains −
var result = collection1.Where(x => !collection2.Contains(x));
Using the Except Operator
The Except operator returns all items from the first collection that are not present in the second collection −
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main(string[] args) {
var listA = Enumerable.Range(1, 6);
var listB = new List<int> { 3, 4 };
var listC = listA.Except(listB);
Console.WriteLine("Items in listA but not in listB:");
foreach (var item in listC) {
Console.WriteLine(item);
}
}
}
The output of the above code is −
Items in listA but not in listB: 1 2 5 6
Using Where with Any
This approach uses LINQ query syntax with the Any method to check if an item exists in the exclusion list −
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main(string[] args) {
var listA = Enumerable.Range(1, 6);
var listB = new List<int> { 3, 4 };
var listC = from c in listA
where !listB.Any(o => o == c)
select c;
Console.WriteLine("Using query syntax with Any:");
foreach (var item in listC) {
Console.WriteLine(item);
}
}
}
The output of the above code is −
Using query syntax with Any: 1 2 5 6
Using Where with Contains
The Contains method provides another way to perform "not in" queries −
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main(string[] args) {
var names = new List<string> { "Alice", "Bob", "Charlie", "David", "Eve" };
var excludeNames = new List<string> { "Bob", "David" };
var filteredNames = names.Where(name => !excludeNames.Contains(name));
Console.WriteLine("Names not in exclude list:");
foreach (var name in filteredNames) {
Console.WriteLine(name);
}
}
}
The output of the above code is −
Names not in exclude list: Alice Charlie Eve
Comparison of Approaches
| Method | Performance | Use Case |
|---|---|---|
| Except | Good for large collections | Simple exclusion of duplicate values |
| Where + Any | Slower for large collections | Complex conditions or custom comparisons |
| Where + Contains | Good for small exclusion lists | Simple membership testing |
Conclusion
LINQ provides multiple ways to perform "not in" queries in C#. The Except operator is typically the most efficient for simple exclusions, while Where with Any or Contains offers more flexibility for complex filtering scenarios.
