c# find difference between two lists of different types code example

Example 1: c# how to check if two lists have same values

var list1 = new List<int> { 1, 2, 3, 1 };
var list2 = new List<int> { 2, 1, 3, 1 };
var list3 = new List<int> { 2, 2, 3, 2 };
bool areTheSame1 = list1.SequenceEqualsIgnoreOrder(list2); //True
bool areTheSame2 = list1.SequenceEqual(list2); //True
bool areTheSame3 = list1.SequenceEqual(list3); //False

Example 2: 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