This post will discuss how to find the set difference between the two lists in C#.

The solution should compare two lists, and return those in the first list but not in the second list and vice versa. We can use LINQ query operations to solve this in C# easily.

1. Using Except() method

The recommended solution is to use LINQ’s Except() method to find the elements of a sequence that don’t appear in the other sequence. This can be used as follows:

Download  Run Code

2. Using Where() with Any() method

The following code example demonstrates how to use the Where() method in combination with the Any() method to find the elements of a sequence that don’t appear in the other sequence.

Download  Run Code

3. Using Where() with Contains() method

The following code example demonstrates how to use the Where() method in combination with the Contains() method to find the elements of a sequence that don’t appear in the other sequence.

Download  Run Code

That’s all about finding the set difference between two lists in C#.