Create duplicate items in a list

后端 未结 3 1700
再見小時候
再見小時候 2021-01-05 16:08

I have the following code to duplicate the members of a list X times.

Although it works it doesn\'t feel particularly clean.

Live code example:

3条回答
  •  再見小時候
    2021-01-05 16:36

    Perhaps something along the lines of:

    var serviceEndPoints = splitEndPoints.SelectMany(t =>
        Enumerable.Repeat(t, instances)).ToList();
    

    That will give you "A,A,A,B,B,B,C,C,C". If you want "A,B,C,A,B,C,A,B,C":

    var serviceEndPoints = Enumerable.Repeat(
        splitEndPoints, instances).SelectMany(t => t).ToList();
    

提交回复
热议问题