【问题标题】:Add two arraylists in a datatable在数据表中添加两个数组列表
【发布时间】:2010-12-26 16:28:12
【问题描述】:

我有两个数组列表。 一个包含文件名,另一个包含失败计数。 这些值来自后面代码中的数据库表。

现在我想将这两个数组列表添加到一个数据表中。 有没有办法做到这一点?

【问题讨论】:

  • 您需要提供更多信息,TimothyP 的解决方案应该适用于当前信息。

标签: c# asp.net datatable arraylist


【解决方案1】:

你提供的信息比较有限,所以'我在这里做一个疯狂的猜测。

var table = new DataTable();
table.Columns.Add("value1");
table.Columns.Add("value2");

for (int i = 0; i < arrayListOne.Count; i++)
{
    var row = table.NewRow();
    row["value1"] = arrayListOne[i];
    row["value2"] = arrayListTwo[i];
    table.Rows.Add(row);
}

当然,这只有在两个列表长度相同时才有效。

如果这不是您想要的,则必须添加更多信息。

【讨论】:

    【解决方案2】:

    这里有很多问题。首先,当然,对于 .Net 2.0 及更高版本,您应该不要再使用 ArrayLists。请改用通用List&lt;T&gt;s。其次,同样重要的是,将它们放入同一个数据表的最佳方法是重写用于生成它们的 sql,这样你就只有一个,而不是两个查询。最后,您没有共享任何代码。如果看不到您使用的代码,我们将如何帮助您解决问题?

    【讨论】:

      【解决方案3】:

      是的,有很多方法可以将两个 ArrayList 实例添加到一个 DataTable 实例中。下面是一种方法,展示了如何使用编译到 .NET Framework 2.0 的完整代码示例将任意数量的 ArrayList 添加到一个 DataTable。

      注意:其他人在尝试破译你在做什么方面做得很好 - 我将直接用完整的代码 sn-p 回答这个问题,以防你获得从中获得任何见解。

      这确实不是一个复杂的答案 - 它只是首先设置几个 ArrayList 实例,其中包含与您的问题相关的示例数据,并在最后提供一个 sn-p 来测试解决方案。

      如果有的话,请评论您在此处找到的见解。谢谢。

      namespace Com.StackOverflow {
      
          using System.Diagnostics;
          using System.Data;
          using System.Collections;
      
          public static class SOQuestion__Add_two_arraylists_in_a_datatable {
      
              public static void AnswerQuestionDirectly() {
      
                  // - - - - - - - - Prelimary setup to question - - - - - - - -
      
                  // Sample list of filenames (3 elements in total).
                  ArrayList listFilenames = new ArrayList( new[] {@"C:\a.dat", @"C:\b.dat", @"C:\c.dat"} );
      
                  // Sample list of error counts (2 elements in total).
                  ArrayList listFailureCounts = new ArrayList( new[] {88,51} );
      
                  // - - - - - - A Direct answer to the question - - - - - - -
      
                  // Create DataTable structure with one column.
                  DataTable dTable = new DataTable();
                  dTable.Columns.Add("Column 1");
      
                  /* Populate DataTable with all lists at once. 
                   * Note: keep appending 
                   * to { array initialization list } all lists you want included
                   * in the DataTable instance.
                   */
                  foreach (ArrayList currentList in new[] { listFilenames, listFailureCounts }) {
                      foreach (object element in currentList)
                          dTable.Rows.Add(new[] { element }); // one column per row
                  }
      
                  // - - - - - - - - Test it for expected counts - - - - - - - -
      
                  int totalExpected = listFilenames.Count + listFailureCounts.Count;
                  //Verify DataTable contains the same amount of rows.
                  Debug.Assert(dTable.Rows.Count == totalExpected);
              }
          }
      }
      

      上面的答案可以复制粘贴到 .cs 文件中,并使用以下代码运行:using Com.StackOverflow;

      SOQuestion__Add_two_arraylists_in_a_datatable.AnswerQuestionDirectly();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-01
        • 1970-01-01
        • 2015-08-25
        • 2013-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多