【问题标题】:Creating a new list by incrementing two separate lists通过增加两个单独的列表来创建新列表
【发布时间】:2019-07-22 00:32:12
【问题描述】:

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

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

【问题讨论】:

    标签: c# linq collections console


    【解决方案1】:

    将从第一个集合中选择的所有元素的值与另一个集合中包含的元素组合:

    var combined = sCat.SelectMany(s => sLev.Select(s1 => $"{s}-{s1}")).ToList();
    

    这就像在嵌套的for/foreach 循环中迭代两个集合,将每个组合元素添加到一个新的List&lt;string&gt;:

    List<string> combined = new List<string>();
    
    foreach (var s1 in sCat)
    foreach (var s2 in sLev) {
            combined.Add(s1 + "-" + s2);
    }
    

    【讨论】:

      【解决方案2】:

      您可以将 for 循环替换为以下内容:

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

      【讨论】:

        【解决方案3】:
            private static void Main()
            {
                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");
        
                string dash = "-";
                List<string> newList = new List<string>();
        
                for (int i = 0; i < sCat.Count; i++)
                {
                    for (int j = 0; j < sLev.Count; j++)
                    {
                        newList.Add(sCat[i] + dash + sLev[j]);
                    }
                }
        
                foreach (var item in newList)
                {
                    Console.WriteLine(item);
                }
        
                Console.ReadLine();
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-01-13
          • 1970-01-01
          • 1970-01-01
          • 2019-04-14
          • 1970-01-01
          • 2019-10-26
          • 1970-01-01
          相关资源
          最近更新 更多