【问题标题】:How do I combine 3 separate lists into 1 list with 3 columns or fields?如何将 3 个单独的列表合并为 1 个包含 3 个列或字段的列表?
【发布时间】:2021-03-15 09:13:58
【问题描述】:

如何在 c# 中将 3 个单独的列表合并为一个具有 3 个列或字段的列表?

我尝试使用 3 个 foreach 循环将 3 个单独的列表添加/组合到每个列或字段到一个列表中,但它一次只添加一个列/字段,并且列表是我需要的 3 倍是并且只有一列/字段的数据,而其他两列/字段为空或零。像这样:

0 0 3.701

0 0 3.633

0 0 3.622

0 0 3.623

我需要它是这样的:

12 2020 3.623 

这是我的代码。

        List<GasPrices> listOfGasPrices = new List<GasPrices>()
        {
        new GasPrices() { Month = 11, Year = 1942, Price = 3.333 }

        };

        foreach (var month in intMonthList)
        {
            listOfGasPrices.Add(new GasPrices { Month = month }); 
            
        }

        foreach (var year in intYearList)
        {
            listOfGasPrices.Add(new GasPrices { Year = year });

        }

        foreach (var price in doublePriceList)
        {
            listOfGasPrices.Add(new GasPrices { Price = price });
        }

        for (int k = 0; k < listOfGasPrices.Count; k++)
        {
            Console.WriteLine(listOfGasPrices[k].Month + " " + listOfGasPrices[k].Year + " " + listOfGasPrices[k].Price);
        }
    


class GasPrices
{
    //backing fields
    private int _month, _year;
    private double _price;

    //constructor
    public GasPrices()
    {
        _month=0;
        _year=0;
        _price=0;
    }

    //getter and setters of properties
    public int Month { get; set; }
    public int Year { get; set; }
    public double Price { get; set; }

}

【问题讨论】:

    标签: c# list class foreach


    【解决方案1】:

    请添加有关如何配置月、年和价目表的更多详细信息。

    • 假设 intYearList 有一个年份范围:例如1950-2020

    • 假设 intMonthList 有 12 个月,即 1-12

    • 假设 doublePriceList 是一个平面系列。

       int yearIndex = 0;        
      
       foreach(var year in intYearList)
       {
          int monthIndex = 0;
          foreach(var month in intMonthList)
          { 
            var priceIndex = yearIndex*12 + monthIndex;
            var price = doublePriceList[priceIndex];
      
            listOfGasPrices.Add(new GasPrices { Month = month, Year = year, Price = price });
      
            monthIndex++;
      
          }
      
           yearIndex++;
      
        }
      

    【讨论】:

      【解决方案2】:

      不要这样使用三个 foreach 循环,而是使用以下代码

      List<int> intMonthList = new List<int>() { 1, 2, 3 }; // Taking sample values
      List<int> intYearList = new List<int>() { 2019, 2020, 2021 };
      List<double> doublePriceList = new List<double>() { 7.1, 8.2, 9.3 };
      
      List<GasPrices> listOfGasPrices = new List<GasPrices>()
      {
          new GasPrices() { Month = 11, Year = 1942, Price = 3.333 }
      
      };
      
      using(var month = intMonthList.GetEnumerator())
      using(var year = intYearList.GetEnumerator())
      using(var price = doublePriceList.GetEnumerator())
      {
          while(month.MoveNext() && year.MoveNext() && price.MoveNext())
          {
              listOfGasPrices.Add(new GasPrices { Month = month.Current, Year = year.Current, Price = price.Current });
      
          }
      }
      
      for (int k = 0; k < listOfGasPrices.Count; k++)
      {
          Console.WriteLine(listOfGasPrices[k].Month + " " + listOfGasPrices[k].Year + " " + listOfGasPrices[k].Price);
      }
      

      【讨论】:

      • 这是一个很好的解决方案,但您可能需要更新您的解决方案,以便在每一年的所有 12 个月内循环运行。
      • 据我从他的问题中了解到,他只是想将 3 个单独列表的答案合并到一个列表中,所以我这样做了,我不知道他是否特别需要所有 12 个的解决方案给定年份的几个月。如果他需要,那么您使用嵌套 foreach 循环的答案就可以了
      • 这也是一个好点。我们不确定列表是如何配置的,因此很难添加精确的解决方案。
      • 所有 3 个列表的长度为 1065 项/行,intMonthList 范围为 1-12,intYearList 1993-2013 和 doublePriceList .900-3.999 提前谢谢您。我会尽快尝试这些 =)
      • 谢谢你们! @malik_aurko96 的代码正是我需要的 =)
      猜你喜欢
      • 2016-04-18
      • 1970-01-01
      • 2019-02-05
      • 2023-02-12
      • 2022-07-13
      • 1970-01-01
      • 2017-10-26
      • 2012-01-27
      • 1970-01-01
      相关资源
      最近更新 更多