繁体   English   中英

字符串数组列表到数组每个元素的数组

[英]List of Array of string to Array of Each element of Array

所以我有一个字符串数组列表,如下所示:

        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" });

我希望他们将它们转换为单独的 arrays ,例如:

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

我已经通过使用foreach列表来实现它,但我想知道是否有任何有效的方法可以做到这一点,因为我在原始List中有 2000 万个项目

这个解决方案启发了我: Rotate - Transposing a List<List<string>> using 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM