小编spi*_*iry的帖子

加入两个不同长度的列表

我正在使用LINQ:

List<String> listA = new List<string>{"a", "b", "c", "d", "e", "f", "g"};  
List<String> listB = new List<string>{"1", "2", "3"};  
Run Code Online (Sandbox Code Playgroud)

期望的结果:

{"a", "1", "b", "2", "c", "3", "d", "1", "e", "2", "f", "3", "g", "1"}  
Run Code Online (Sandbox Code Playgroud)

我试过但失败了:

var mix = ListA.Zip(ListB, (l1, l2) => new[] { l1, l2 }).SelectMany(x => x);  
//Result : {"a", "1", "b", "2", "c", "3"}  

var mix = ListA.Zip(ListB, (a, b) => new[] { a, b })
        .SelectMany(x => x)
        .Concat(ListA.Count() < ListB.Count() ? ListB.Skip(ListA.Count()) : ListA.Skip(ListB.Count()))
        .ToList(); …
Run Code Online (Sandbox Code Playgroud)

c# linq

9
推荐指数
1
解决办法
331
查看次数

标签 统计

c# ×1

linq ×1