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 compare two lists and add the difference to a third list in C#?
When working with lists in C#, you often need to compare two lists and find the elements that exist in one list but not in another. This is commonly done using LINQ's Except method, which returns the set difference between two sequences.
Syntax
Following is the syntax for using the Except method to find differences between two lists −
IEnumerable<T> result = list1.Except(list2);
To store the result in a new list −
List<T> differenceList = list1.Except(list2).ToList();
Using Except Method to Find Differences
The Except method returns elements from the first list that are not present in the second list. This creates a new sequence containing the difference −
Example
using System;
using System.Collections.Generic;
using System.Linq;
public class Demo {
public static void Main() {
List<string> list1 = new List<string> {"A", "B", "C", "D"};
List<string> list2 = new List<string> {"C", "D"};
Console.WriteLine("First list...");
foreach(string value in list1) {
Console.WriteLine(value);
}
Console.WriteLine("Second list...");
foreach(string value in list2) {
Console.WriteLine(value);
}
Console.WriteLine("Difference (elements in list1 but not in list2)...");
IEnumerable<string> list3 = list1.Except(list2);
foreach(string value in list3) {
Console.WriteLine(value);
}
}
}
The output of the above code is −
First list... A B C D Second list... C D Difference (elements in list1 but not in list2)... A B
Creating a Third List with Differences
Instead of using IEnumerable, you can convert the result to a concrete List using the ToList() method −
Example
using System;
using System.Collections.Generic;
using System.Linq;
public class ListComparison {
public static void Main() {
List<int> numbers1 = new List<int> {1, 2, 3, 4, 5, 6};
List<int> numbers2 = new List<int> {3, 4, 5, 7, 8};
// Create a third list containing the difference
List<int> differenceList = numbers1.Except(numbers2).ToList();
Console.WriteLine("Numbers in list1 but not in list2:");
foreach(int number in differenceList) {
Console.WriteLine(number);
}
Console.WriteLine($"Total unique elements: {differenceList.Count}");
}
}
The output of the above code is −
Numbers in list1 but not in list2: 1 2 6 Total unique elements: 3
Finding Symmetric Difference
To find elements that are in either list but not in both (symmetric difference), you can combine both directions using Union −
Example
using System;
using System.Collections.Generic;
using System.Linq;
public class SymmetricDifference {
public static void Main() {
List<string> fruits1 = new List<string> {"Apple", "Banana", "Orange", "Mango"};
List<string> fruits2 = new List<string> {"Orange", "Mango", "Grape", "Pineapple"};
// Elements in fruits1 but not in fruits2
var diff1 = fruits1.Except(fruits2);
// Elements in fruits2 but not in fruits1
var diff2 = fruits2.Except(fruits1);
// Combine both differences
List<string> symmetricDiff = diff1.Union(diff2).ToList();
Console.WriteLine("Symmetric difference (elements in either list but not both):");
foreach(string fruit in symmetricDiff) {
Console.WriteLine(fruit);
}
}
}
The output of the above code is −
Symmetric difference (elements in either list but not both): Apple Banana Grape Pineapple
Comparison of Methods
| Method | Purpose | Result |
|---|---|---|
list1.Except(list2) |
Elements in list1 but not in list2 | One-way difference |
list1.Except(list2).Union(list2.Except(list1)) |
Elements in either list but not both | Symmetric difference |
list1.Intersect(list2) |
Elements common to both lists | Intersection |
Conclusion
The Except method in LINQ provides an efficient way to compare two lists and find the difference. Use ToList() to convert the result to a concrete list, and combine Except with Union to find symmetric differences between lists.
