Suppose we have 2 Lists of Products, and we want to make it one List.
List<Product> products1 = GetProducts(cid1); List<Product> products2 = GetProducts(cid2);
One way is to iterate the 2nd List, and add all elements to the 1st List,
or iterate both Lists and populate the other List with elements in both Lists.
Does .NET Framework provide some API to do that…?
The answer is YES!.
There are two ways to do that.
Using LINQ:
List<Product> allProducts = products1.Concat(products2).ToList();
Using AddRange (IEnumerable) :
products1.AddRange(products2);
It will not disturb the order of the list and remember, we didn’t ask it to remove any duplications. Here’s how we can merge by removing duplicates.
As shown in the above code lines, we can similarly merge multiple Lists into one list just by using any of the above-mentioned approaches.