【问题标题】:LINQ Select from two Collections with same lengthLINQ 从具有相同长度的两个集合中选择
【发布时间】:2015-11-28 15:06:40
【问题描述】:
var listA = new List<string> {"One", "Two", "Three"};
var listB = new List<int> {1, 2, 3};
var listC = new List<Tuple<string, int>>();

这里有两个长度相同的列表。有没有办法用 LINQ 填充第三个listC

"One", 1
"Two", 2
"Three", 3

【问题讨论】:

    标签: c# linq list select collections


    【解决方案1】:

    我认为您正在寻找Zip 扩展方法:

    var listA = new List<string> { "One", "Two", "Three" };
    var listB = new List<int> { 1, 2, 3 };
    var listC = listA.Zip(listB, (s, i) => new Tuple<string, int>(s, i)).ToList();
    

    【讨论】:

      猜你喜欢
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-27
      • 2022-08-18
      • 1970-01-01
      • 2020-01-03
      • 1970-01-01
      相关资源
      最近更新 更多