C# 将多个列表垂直合并为单个字符串列表

C# 将多个列表垂直合并为单个字符串列表,c#,C#,我有以下字符串列表 List<string> List1 = new List<string> { "P1", "P2", "P3" }; List<string> List2 = new List<string> { "Q1", "Q2", "Q3" }; List<string> List3 = new List<string> { "R1", "R2", "R3" }; //........ // Add List1

我有以下字符串列表

List<string> List1 = new List<string> { "P1", "P2", "P3" };
List<string> List2 = new List<string> { "Q1", "Q2", "Q3" };
List<string> List3 = new List<string> { "R1", "R2", "R3" };

//........
// Add List1,List2, List3 values Vertically  to CombileList

CombineList = { "P1", "Q1", "R1", "P2", "Q2", "R2", "P3", "Q3", "R3" };
List List1=新列表{“P1”、“P2”、“P3”};
List List2=新列表{“Q1”、“Q2”、“Q3”};
List List3=新列表{“R1”、“R2”、“R3”};
//........
//将列表1、列表2、列表3的值垂直添加到CombileList
组合列表={“P1”、“Q1”、“R1”、“P2”、“Q2”、“R2”、“P3”、“Q3”、“R3”};

我想从所有列表中垂直向CombineList添加值,如CombineList中所示,可以以相同的方式向CombineList添加n个列表。

如果列表大小相同,可以使用for循环:

List<string> list1 = new List<string> { "P1", "P2", "P3" };
List<string> list2 = new List<string> { "Q1", "Q2", "Q3" };
List<string> list3 = new List<string> { "R1", "R2", "R3" };

List<string> combinedList = new List<string>();

for(int i = 0; i < list1.Count; i++)
{
    combinedList.Add(list1[i]);
    combinedList.Add(list2[i]);
    combinedList.Add(list3[i]);
}
List list1=新列表{“P1”、“P2”、“P3”};
List list2=新列表{“Q1”、“Q2”、“Q3”};
List list3=新列表{“R1”、“R2”、“R3”};
List combinedList=新列表();
for(int i=0;i
提出了类似的问题

使用
IEnumerator
MoveNext()
方法,您可以通过使用枚举器在数组上循环并按自己喜欢的方式组合它们:

public List<T> CombineVertically<T>(List<List<T>> Source)
        {
            List<T> result = new List<T>();

            var enumerators = Source.Select(x => x.GetEnumerator());
            while (enumerators.Where(x => x.MoveNext()).Count() > 0)            
                result.AddRange(enumerators.Select(x => x.Current));

            enumerators.ToList()
                .ForEach(x => x.Dispose());

            return result;
        }
公共列表组合(列表源)
{
列表结果=新列表();
var enumerators=Source.Select(x=>x.GetEnumerator());
while(枚举数.Where(x=>x.MoveNext()).Count()>0)
AddRange(枚举数.Select(x=>x.Current));
枚举数.ToList()
.ForEach(x=>x.Dispose());
返回结果;
}

您试过什么吗?使用循环应该不会太难。这个答案可能就是您想要的:
IEnumerator
implements
IDisposable
,不要忘记
Dispose
instances@DmitryBychenko你完全正确