Linq,来自 2 个列表中的项目元素



我有一个类:

public class Composite
{
    int Width { get; set; }
    int Distance { get; set; }
}

和 2 个列表:

var widths = new List<int>(new[] {0, 1, 2, 3, 4 } );
var distances = new List<int>(new[] { 5, 6, 7, 8, 9 });

我想通过使用源列表中的相同索引组合宽度和距离来创建 5 个复合实例:

  • 0 & 5
  • 1 & 6
  • 2 & 7
  • 3 & 8
  • 4 & 9

我猜某种连接选择到复合中,但由源列表的索引连接。谁能帮忙?

var composites = widths.Zip(distances, (w, d) => new Composite() { Width = w, Distance = d })
                       .ToList();

最新更新