加入两个不同长度的列表

编程入门 行业动态 更新时间:2024-10-06 18:30:43
本文介绍了加入两个不同长度的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用LINQ:

List<String> listA = new List<string>{"a", "b", "c", "d", "e", "f", "g"}; List<String> listB = new List<string>{"1", "2", "3"};

所需结果:

{"a", "1", "b", "2", "c", "3", "d", "1", "e", "2", "f", "3", "g", "1"}

我尝试但失败了:

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(); //Result : {"a", "1", "b", "2", "c", "3", "d", "e", "f", "g"}

如何使用LINQ做到这一点?

How can I do this using LINQ?

推荐答案

即使我不确定为什么您需要它作为linq表达式,此方法仍然有效:

This works, even if I am not sure why you need it as linq expression:

var mix = Enumerable .Range(0, Math.Max(listA.Count, listB.Count)) .Select(i => new[] { listA[i % listA.Count], listB[i % listB.Count] }) .SelectMany(x => x);

更多推荐

加入两个不同长度的列表

本文发布于:2023-11-29 15:37:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646818.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:长度   两个   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!