For example, I have a list
var list = new List<int> { 1, 2, 3 };
and I want to repeat its content 3 times to get in result
{ 1, 2, 3, 1, 2, 3, 1, 2, 3 }
Is there a simpler solution than just AddRange
m times in for loop?
CodePudding user response:
using Enumerable.Repeat
var repeatCount = 3; //change this value to repeat n times
var list = new List<int> { 1, 2, 3 };
var result = Enumerable.Repeat(list, repeatCount).SelectMany(x => x).ToList();
CodePudding user response:
You can use LINQ's Enumerable.Range
with SelectMany
:
list = Enumerable.Range(0, 3)
.SelectMany(_ => list)
.ToList();
This will differ from adding to the list though - new list will be created here.