将两个锯齿状列表合并为一个 [英] Combine two jagged lists into one

查看:92
本文介绍了将两个锯齿状列表合并为一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个锯齿状的列表。

Im having two jagged lists.

第一个:

List<List<string>> firstList = new List<List<string>>();

{ dd ff }
{ dd ff }
{ dd ff }

第二个:

List<List<string>> secondList = new List<List<string>>();

{ gg hh }
{ gg uu }
{ hh uu }

在将两个列表组合成这样的列表时遇到问题:

Im having a problem with combining both lists into one like this one:

{ dd ff gg hh }
{ dd ff gg uu }
{ dd ff hh uu }

任何串联尝试都只会添加 secondList 元素,例如另一个 firstList 行,例如:

Any Concatenation attempt resulted in just adding secondList elements like another firstList rows like this:

{ dd ff }
{ dd ff }
{ dd ff }
{ gg hh }
{ gg uu }
{ hh uu }

希望对此有所帮助!

推荐答案

您可以使用Zip扩展方法,该方法是 System.Linq 的一部分命名空间。元组可用于关联两个列表中的值。或者,您也可以只 Concat 内部列表。

You can use Zip extension method that is a part of the System.Linq namespace. Tuples can be used to associate values form both lists. Or you can just Concat inner lists.

var list1 = new List<List<string>>
{
    new List<string> { "dd", "ff" },
    new List<string> { "dd", "ff" },
    new List<string> { "dd", "ff" },
};

var list2 = new List<List<string>>
{
    new List<string> { "gg", "hh" },
    new List<string> { "gg", "uu" },
    new List<string> { "hh", "uu" },
};

var result = list1.Zip(list2, (l1, l2) => Tuple.Create(l1, l2)).ToList();

这篇关于将两个锯齿状列表合并为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆