Search code examples
c#linqcollectionsconsole

Creating a new list by incrementing two separate lists


I'm trying to create a new list from two separate lists like so:

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...

I would like to know if there is a function that would produce the results above. Ultimately, I would like the user to provide List 2 and have that information added to List 1 and stored as a new list that I could call later.

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();
    }
}

Solution

  • You can replace your for loop with following:

    foreach(var sCatValue in sCat)
    {
        foreach(var sLevValue in sLev)
        {
            Console.WriteLine($"{sCatValue}-{sLevValue}");
        }
    }