Список массива строк в массив каждого элемента массива - PullRequest
0 голосов
/ 08 апреля 2020

Итак, у меня есть список массивов строк, как показано ниже:

        List<string[]> ArrayList = new List<string[]>();
        ArrayList.Add(new string[] { "7112432","Gagan","Human Resource"});
        ArrayList.Add(new string[] { "7112433", "Mukesh", "Information Technology" });
        ArrayList.Add(new string[] { "7112434", "Steve", "Human Resource" });
        ArrayList.Add(new string[] { "7112435", "Trish", "Human Resource" });

Я хочу, чтобы они преобразовали их в отдельные массивы, такие как:

        EmployeeNumber=["7112432","7112433","7112434","7112435"]
        Name =["Gagan", "Mukesh", "Steve", "Trish"]
        Department =["Human Resource", "Information Technology", "Human Resource", "Human Resource"]

Я добился этого путем зацикливания через список, используя foreach, но я хочу знать, есть ли какой-нибудь эффективный способ сделать это, потому что у меня есть как 20 миллионов предметов в оригинальном List

1 Ответ

0 голосов
/ 08 апреля 2020

Это решение вдохновило меня: Поворот - Перенос списка используя LINQ C#

Вот код, который вы можете использовать для своих нужд:


        List<string[]> ArrayList = new List<string[]>();
        for (int i = 0; i < 20000000; i++)
        {
            //The simulation of the 20.000.000 arrays of the list takes some time (+- 10s)...
            //But you already have the list so you can skip this part of the code
            ArrayList.Add(new string[] { i.ToString(), "Gagan", "Human Resource", "AAA" });
        }

        var millis = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

        List<List<string>> results = ArrayList.SelectMany
        (theArray => theArray.Select((itemInArray, indexInArray) => new { itemInArray, indexInArray })) //All single elements
        .GroupBy(i => i.indexInArray, i => i.itemInArray) //Group them
        .Select(g => g.ToList())
        .ToList();

        var seconds = (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - millis) / 1000d; //9.8 s
...