例如,我有一个 IEnumerable<(int, char)> list
.如何转换list
进入(IEnumerable<int>, IEnumerable<char>)
?
有没有快速的方法来做到这一点?使用System.Linq
会更好.
最佳答案
聚合非常简单:
IEnumerable<(int, char)> list = new[]
{
(1, 'a'), (2, 'b'),
};
(List<int> ints, List<char> chars) =
list.Aggregate((new List<int>(), new List<char>()), (a, x) =>
{
a.Item1.Add(x.Item1);
a.Item2.Add(x.Item2);
return a;
});
这给出了:
这是最快的方法,但这个更简单:
List<int> ints = list.Select(x => x.Item1).ToList();
List<char> chars = list.Select(x => x.Item2).ToList();
关于c# - 如何将元组列表转换为列表元组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73244842/