Merge two (or more) lists into one, in C# .NET

You can use the LINQ Concat and ToList methods:

var allProducts = productCollection1.Concat(productCollection2)
                                    .Concat(productCollection3)
                                    .ToList();

Note that there are more efficient ways to do this – the above will basically loop through all the entries, creating a dynamically sized buffer. As you can predict the size to start with, you don’t need this dynamic sizing… so you could use:

var allProducts = new List<Product>(productCollection1.Count +
                                    productCollection2.Count +
                                    productCollection3.Count);
allProducts.AddRange(productCollection1);
allProducts.AddRange(productCollection2);
allProducts.AddRange(productCollection3);

(AddRange is special-cased for ICollection<T> for efficiency.)

I wouldn’t take this approach unless you really have to though.

Leave a Comment