'Create duplicate items and submit them inside a list

Hello let me show you the problem with very simple explanation.

I have this;

int[] numbers1= { 1, 2, 3 };

and i need this;

int[] numbers2= { 1,1,1, 2,2,2, 3,3,3 };

how can i duplicate my values then use them in another list?



Solution 1:[1]

Try:

using System.Linq;

namespace WebJob1
{
    internal class Program
    {

        static void Main()
        {
            int[] numbers1 = { 1, 2, 3 };
            var numbers2 = numbers1.SelectMany(x => Enumerable.Repeat(x,3)).ToArray();
        }
    }
}

Solution 2:[2]

You can do this with LINQ

var numbers2 = numbers1.SelectMany(x => new[]{x,x,x});

Live example: https://dotnetfiddle.net/FLAQIY

Solution 3:[3]

You can try this:

    int multiplier = 3;
    int[] numbers1 = { 1, 2, 3 };

    var numbers2 = numbers1.SelectMany(x => Enumerable.Repeat(x, multiplier)).ToArray();

Maybe some useful information about LINQ extentions Select and SelectMany here.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mykyta Halchenko
Solution 2 Jamiec
Solution 3 Dominik