I have 4 collections containing strings, like this:
first: xxxxxxxx second: xx third: xxxxxxxx fourth: xx
I want to concat these 4 collections to get: xxxxxxxxxxxxxxxxxxxx
I wanted to use the Enumerable.Concat but I have one problem with this, It only take two parameters. As a consequence I can concat only the first with the second (for example) but not all of'em together.
Is c# offers a way to concat more than 2 collections together?
Am I having to create two collections which concat first + second and third + fourth and then concat the two collections together?
EDIT
I made a mistake, It's 4 collections I want to concat. These collections contains strings like shown before, but these are Collections
You can chain the Enumerable.Concat
calls like this.
List<string> list1 = new List<string>();
List<string> list2 = new List<string>();
List<string> list3 = new List<string>();
List<string> list4 = new List<string>();
//Populate the lists
var mergedList = list1.Concat(list2)
.Concat(list3)
.Concat(list4)
.ToList();
Another option is to create an array and call SelectMany
var mergedList = new[]
{
list1, list2, list3, list4
}
.SelectMany(x => x)
.ToList();
Note: Enumerable.Concat
will allow duplicate elements, if you want to eliminate duplicates you can use Enumerable.Union
method, rest all same.