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 find items in one list that are not in another list in C#?
Finding items in one list that are not present in another list is a common task in C# programming. LINQ provides multiple approaches to solve this problem, with the Except() method being the most straightforward for simple data types and the Where() clause being more flexible for complex objects.
Syntax
Using the Except() method for simple types −
var result = list1.Except(list2);
Using Where() clause with All() for complex objects −
var result = list1.Where(item1 => list2.All(item2 => condition));
Using Except() Method
The Except() method is a LINQ set operator that compares two collections and returns elements from the first collection that are not present in the second collection. This method works well for primitive data types and simple comparisons −
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main(string[] args) {
List<string> animalsList1 = new List<string> {
"tiger", "lion", "dog"
};
Console.WriteLine("Values in List1:");
foreach (var val in animalsList1) {
Console.WriteLine(val);
}
List<string> animalsList2 = new List<string> {
"tiger", "cat", "deer"
};
Console.WriteLine("Values in List2:");
foreach (var val in animalsList2) {
Console.WriteLine(val);
}
var animalsList3 = animalsList1.Except(animalsList2);
Console.WriteLine("Values in List1 that are not in List2:");
foreach (var val in animalsList3) {
Console.WriteLine(val);
}
}
}
The output of the above code is −
Values in List1: tiger lion dog Values in List2: tiger cat deer Values in List1 that are not in List2: lion dog
Using Where Clause with Complex Objects
For complex objects, the Except() method may not work as expected because it compares object references rather than property values. In such cases, the Where() clause combined with All() provides more control over the comparison logic −
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main(string[] args) {
List<Fruit> fruitsList1 = new List<Fruit> {
new Fruit {
Name = "Apple",
Size = "Small"
},
new Fruit {
Name = "Orange",
Size = "Small"
}
};
Console.WriteLine("Values in List1:");
foreach (var val in fruitsList1) {
Console.WriteLine(val.Name);
}
List<Fruit> fruitsList2 = new List<Fruit> {
new Fruit {
Name = "Apple",
Size = "Small"
},
new Fruit {
Name = "Mango",
Size = "Small"
}
};
Console.WriteLine("Values in List2:");
foreach (var val in fruitsList2) {
Console.WriteLine(val.Name);
}
var fruitsList3 = fruitsList1.Where(f1 => fruitsList2.All(f2 => f2.Name != f1.Name));
Console.WriteLine("Values in List1 that are not in List2:");
foreach (var val in fruitsList3) {
Console.WriteLine(val.Name);
}
}
}
public class Fruit {
public string Name { get; set; }
public string Size { get; set; }
}
The output of the above code is −
Values in List1: Apple Orange Values in List2: Apple Mango Values in List1 that are not in List2: Orange
Using Custom Equality Comparer
For complex objects, you can also create a custom equality comparer to use with the Except() method −
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main(string[] args) {
List<Product> products1 = new List<Product> {
new Product { Id = 1, Name = "Laptop" },
new Product { Id = 2, Name = "Mouse" }
};
List<Product> products2 = new List<Product> {
new Product { Id = 1, Name = "Laptop" },
new Product { Id = 3, Name = "Keyboard" }
};
var uniqueProducts = products1.Except(products2, new ProductComparer());
Console.WriteLine("Products in List1 that are not in List2:");
foreach (var product in uniqueProducts) {
Console.WriteLine($"{product.Id}: {product.Name}");
}
}
}
public class Product {
public int Id { get; set; }
public string Name { get; set; }
}
public class ProductComparer : IEqualityComparer<Product> {
public bool Equals(Product x, Product y) {
return x.Id == y.Id;
}
public int GetHashCode(Product obj) {
return obj.Id.GetHashCode();
}
}
The output of the above code is −
Products in List1 that are not in List2: 2: Mouse
Comparison
| Method | Best For | Performance | Complexity |
|---|---|---|---|
| Except() | Simple types (string, int, etc.) | High | Low |
| Where() with All() | Complex objects with custom logic | Medium | Medium |
| Except() with Custom Comparer | Complex objects with consistent comparison | High | High |
Conclusion
Use Except() for simple data types and Where() with All() for complex objects where you need custom comparison logic. For complex objects with consistent comparison requirements, consider implementing a custom equality comparer to use with Except() for better performance.
