Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to repeat n values in list m times in C#?

Tags:

arrays

c#

.net

list

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 smarter/shorter solution than just AddRange m times in for loop?

like image 619
majorro Avatar asked Sep 11 '25 19:09

majorro


1 Answers

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();
like image 128
Krishna Varma Avatar answered Sep 14 '25 10:09

Krishna Varma