【问题标题】:looping through a C# collections循环遍历 C# 集合
【发布时间】:2016-07-21 08:38:37
【问题描述】:

我有一个类和方法

public class Datas
    {
        public string Name { get; set; }
        public int Value { get; set; }
    }
 public void Funnel()
    {
        string commandText = "select sc.stagename, count(cs.stages_id) as StageCount from currentstage cs inner join stagesconfig sc on cs.stages_id = sc.stages_id group by cs.stages_id,sc.stagename";


        string constrings = WebConfigurationManager.ConnectionStrings["Data"].ToString();

        SqlConnection myConn = new SqlConnection(constrings);
        SqlCommand myComm = new SqlCommand(commandText, myConn);

        myConn.Open();
        List<Datas> fruitinfo = new List<Datas>();

        SqlDataReader reader = myComm.ExecuteReader();
        if (reader.HasRows)
        {
            while (reader.Read())
            {
                fruitinfo.Add(new Datas
                {
                    Name = reader.GetValue(0).ToString(),
                    Value = Convert.ToInt32(reader.GetValue(1))
                });

            }

        }

如何循环遍历fruitinfo列表,将其保存为数组形式。数组必须与此形式相似。打算将Data括号中的项目替换为fruitinfo循环列表

    Data = new Data(new object[]
                {
                    new object[] { "Website visits", 10000 },
                    new object[] { "Downloads", 5000 },
                    new object[] { "Requested price list", 2000 },
                    new object[] { "Invoice sent", 1000 },
                    new object[] { "Finalized", 500 }
                }),

【问题讨论】:

  • 你为什么需要这个丑陋的object[]

标签: c# collections


【解决方案1】:
var myArray = fruitinfo.Select(x => new object[] { x.Name, x.Value }).ToArray();

并将它与您的Data-object 一起使用。

Data = new Data(myArray);

【讨论】:

    【解决方案2】:
    var myArray = fruitinfo.Select(d => new object[] { d.Name, d.Value }).ToArray();
    

    【讨论】:

      【解决方案3】:

      我不太清楚为什么需要生成一组匿名对象,但可以使用字典。

      private static void Funnel()
          {
              var datas = new List<Datas>
                              {
                                  new Datas { Name = "Website visits", Value = 10000 },
                                  new Datas { Name = "Downloads", Value = 5000 },
                                  new Datas { Name = "Requested price list", Value = 2000 },
                                  new Datas { Name = "Invoice sent", Value = 1000 },
                                  new Datas { Name = "Finalized", Value = 500 }
                              };
      
              var data = datas.ToDictionary(datas1 => datas1.Name, datas1 => datas1.Value);
              foreach (var item in data)
              {
                  Console.WriteLine(string.Format("{0}, {1}",item.Key, item.Value));
              }
      
              var arry = data.ToArray();
              foreach (var item in arry)
              {
                  Console.WriteLine(string.Format("{0}, {1}", item.Key, item.Value));
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-16
        • 2016-07-16
        • 2014-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-28
        相关资源
        最近更新 更多