gpt4 book ai didi

c# - 连接多个 IEnumerable

转载 作者:行者123 更新时间:2023-12-03 03:47:33 26 4
gpt4 key购买 nike

我正在尝试实现一种方法来连接多个List,例如

List<string> l1 = new List<string> { "1", "2" };
List<string> l2 = new List<string> { "1", "2" };
List<string> l3 = new List<string> { "1", "2" };
var result = Concatenate(l1, l2, l3);

但是我的方法不起作用:

public static IEnumerable<T> Concatenate<T>(params IEnumerable<T> List)
{
var temp = List.First();
for (int i = 1; i < List.Count(); i++)
{
temp = Enumerable.Concat(temp, List.ElementAt(i));
}
return temp;
}

最佳答案

使用SelectMany:

public static IEnumerable<T> Concatenate<T>(params IEnumerable<T>[] lists)
{
return lists.SelectMany(x => x);
}

关于c# - 连接多个 IEnumerable<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27056967/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com