Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create duplicate items and submit them inside a list

Tags:

c#

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?

like image 341
MEHMET KURTOĞLU Avatar asked Sep 17 '25 15:09

MEHMET KURTOĞLU


2 Answers

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();
        }
    }
}
like image 124
Mykyta Halchenko Avatar answered Sep 19 '25 06:09

Mykyta Halchenko


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.

like image 28
Dominik Avatar answered Sep 19 '25 06:09

Dominik