通过增加两个单独的列表来创建一个新列表

我正在尝试从两个单独的列表创建一个新列表,如下所示:


List 1 (sCat) = MD0, MD1, MD3, MD4

List 2 (sLev) = 01, 02, 03, R


Output-->

MD0-01

MD0-02

MD0-03

MD0-R

MD1-01

MD1-02

MD1-03

MD1-R

MD3-01

MD3-02

MD3-03

MD3-R

etc...

我想知道是否有一个函数可以产生上述结果。最终,我希望用户提供列表 2,并将该信息添加到列表 1 中,并存储为我稍后可以调用的新列表。


enter code here


using System;

using System.Collections.Generic;

using System.Linq;


class Program

{

    static void Main(string[] args)

    {

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

        // add Categories for the Sheets

        sCat.Add("MD0");

        sCat.Add("MD1");

        sCat.Add("MD3");


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

        // add Levels for the Project

        sLev.Add("01");

        sLev.Add("02");

        sLev.Add("03");

        sLev.Add("R");


        for (int i = 0; i < sCat.Count; i++)

        {

            // I am getting stuck here.

            // I don't know how to take one item from the sCat list and 

            // add it to the sLev List incrementally. 

            Console.WriteLine(sCat[i],i);

        }

        Console.ReadLine();

    }

}


慕少森
浏览 140回答 3
3回答

宝慕林4294392

将从第一个集合中选择的所有元素的值与另一个集合中包含的元素组合起来:var combined = sCat.SelectMany(s => sLev.Select(s1 => $"{s}-{s1}")).ToList();这就像在嵌套for/foreach循环中迭代两个集合,将每个组合元素添加到新的List<string>:List<string> combined = new List<string>();foreach (var s1 in sCat)foreach (var s2 in sLev) {&nbsp; &nbsp; &nbsp; &nbsp; combined.Add(s1 + "-" + s2);}

慕哥6287543

您可以将for循环替换为以下内容:foreach(var sCatValue in sCat){&nbsp; &nbsp; foreach(var sLevValue in sLev)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine($"{sCatValue}-{sLevValue}");&nbsp; &nbsp; }}

桃花长相依

&nbsp; private static void Main()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; List<string> sCat = new List<string>();&nbsp; &nbsp; &nbsp; &nbsp; // add Categories for the Sheets&nbsp; &nbsp; &nbsp; &nbsp; sCat.Add("MD0");&nbsp; &nbsp; &nbsp; &nbsp; sCat.Add("MD1");&nbsp; &nbsp; &nbsp; &nbsp; sCat.Add("MD3");&nbsp; &nbsp; &nbsp; &nbsp; List<string> sLev = new List<string>();&nbsp; &nbsp; &nbsp; &nbsp; // add Levels for the Project&nbsp; &nbsp; &nbsp; &nbsp; sLev.Add("01");&nbsp; &nbsp; &nbsp; &nbsp; sLev.Add("02");&nbsp; &nbsp; &nbsp; &nbsp; sLev.Add("03");&nbsp; &nbsp; &nbsp; &nbsp; sLev.Add("R");&nbsp; &nbsp; &nbsp; &nbsp; string dash = "-";&nbsp; &nbsp; &nbsp; &nbsp; List<string> newList = new List<string>();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < sCat.Count; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < sLev.Count; j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newList.Add(sCat[i] + dash + sLev[j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; foreach (var item in newList)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(item);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP