What is the easiest way to compare the elements of two lists say A and B with one another, and add the elements which are present in B to A only if they are not present in A?
To illustrate, Take list A = {1,2,3} list B = {3,4,5}
So after the operation AUB I want list A = {1,2,3,4,5}
If it is a list, you can also use AddRange method.
var listB = new List<int>{3, 4, 5};
var listA = new List<int>{1, 2, 3, 4, 5};
listA.AddRange(listB); // listA now has elements of listB also.
If you need new list (and exclude the duplicate), you can use Union
var listB = new List<int>{3, 4, 5};
var listA = new List<int>{1, 2, 3, 4, 5};
var listFinal = listA.Union(listB);
If you need new list (and include the duplicate), you can use Concat
var listB = new List<int>{3, 4, 5};
var listA = new List<int>{1, 2, 3, 4, 5};
var listFinal = listA.Concat(listB);
If you need common items, you can use Intersect.
var listB = new List<int>{3, 4, 5};
var listA = new List<int>{1, 2, 3, 4};
var listFinal = listA.Intersect(listB); //3,4