c# compare two lists and get differences code example

Example 1: difference two list c#

var list1 = new List<int> { 1, 2, 3, 4, 5};
var list2 = new List<int> { 3, 4, 5, 6, 7 };

var list3 = list1.Except(list2); //list3 contains only 1, 2
var list4 = list2.Except(list1); //list4 contains only 6, 7
var resultList = list3.Concat(list4).ToList(); //resultList contains 1, 2, 6, 7

Example 2: get waht is differnt between two arrays c#

//get whats in array 2 but not array 1
- array2.Except(array1)
//get whats in array 1 but not array 2
- array1.Except(array2)
//get what is in both array 1 and array 2
- array1.Intersect(array2)