C# 添加 List<string> 到 List<List<string>> 数组

标签 c# arrays list

我可以添加 List<string>List<List<string>>以这种方式排列:

        List<string> first = new List<string> { "one", "two", "three" };
        List<string> second = new List<string> { "four", "five", "six" };

        List<List<string>> list_array = new List<List<string>> { first, second };

现在我需要创建几个填充数据库记录的列表,然后将此列表添加到 List<List<string>>数组:

    List<List<string>> array_list;

            while (dr.Read())
            {
                string one = dr["Row1"].ToString();
                string two = dr["Row2"].ToString();

                List<string> temp_list = new List<string> { one, two };

                //Here I need to add temp_list to array_list
            }

最佳答案

创建一个空的array_list:

List<List<string>> array_list = new List<List<string>>();

然后使用Add方法添加项目:

array_list.Add(temp_list);

关于C# 添加 List<string> 到 List<List<string>> 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14277763/

相关文章:

python - 将两个 LISTS 值的 SUM 添加到新 LIST

c# - 在 wpf 应用程序中运行其他控制台进程之前如何等待控制台进程完成?

c# - 有没有更好的方法来 self 引用类型?

javascript - 在变量中执行循环(for)

c - 当用户在 c 中完成输入时停止 for 循环

php - 将 HTML 表单请求转换为 php 数组

list - Seq 的使用与 List、Array 和 Vector 的使用相比如何?

c# - 在 WebAPI 模型属性中拒绝 HTML

c# - HttpClient 抛出 System.ArgumentException : 'windows-1251' is not a supported encoding name

java - 使用泛型和参数化列表的问题