- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find items in one list that are not in another list in C#?
LINQ Except operator comes under Set operators category in LINQ
The Except() method requires two collections and finding those elements which are not present in the second collection
Except for the extension, the method doesn't return the correct result for the collection of complex types.
Example using Except() method
using System; using System.Collections.Generic; using System.Linq; namespace DemoApplication { 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($"Value in List1 that are not in List2:"); foreach (var val in animalsList3) { Console.WriteLine($"{val}"); } Console.ReadLine(); } } }
Output
The output of the above code is
Values in List1: tiger lion dog Values in List2: tiger cat deer Value in List1 that are not in List2: lion dog
Example using Where Clause
using System; using System.Collections.Generic; using System.Linq; namespace DemoApplication { 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}"); } Console.ReadLine(); } } public class Fruit { public string Name { get; set; } public string Size { get; set; } } }
Output
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
- Related Articles
- How to add items to a list in C#?
- How to check if one list against another in Excel?
- How to remove items from a list in C#?
- How do I copy items from list to list without foreach in C#?
- Program to check linked list items are forming palindrome or not in Python
- How can I sort one list by values from another list in Python?
- How to copy a list to another list in Java?
- What are Inline List Items in CSS
- What are Floating List Items in CSS
- People Whose List of Favorite Companies Is Not a Subset of Another List in C++
- How to remove index list from another list in python?
- Python - First occurrence of one list in another
- Java Program to copy value from one list to another list
- How to count the number of items in a C# list?
- Python - Insert list in another list

Advertisements