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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14277763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:17:14  来源:igfitidea点击:

C# Add List<string> to List<List<string>> array

c#arrayslist

提问by Ljupco Sofijanov

I'm able to add List<string>in List<List<string>>array in this way:

我能添加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 };

Now I need to create several lists populated with database records and then to add this lists to List<List<string>>array:

现在我需要创建几个填充了数据库记录的列表,然后将此列表添加到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
            }

采纳答案by Al Kepp

Create an empty array_list:

创建一个空的array_list:

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

Then use Addmethod to add items:

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

array_list.Add(temp_list);

回答by JMK

This should work:

这应该有效:

array_list.Add(temp_list);

回答by Dave Bish

Change your variable declaration to initialize an empty List:

更改您的变量声明以初始化一个空列表:

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

Then, just call .Add();

然后,只需调用 .Add();

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

//Here I need to add temp_list to array_list
array_list.Add(temp_list);

回答by 1969877

Unless i'm reading this wrong, you should just be able to do:

除非我读错了,否则你应该能够做到:

array_list.add(temp_list);

回答by daryal

you can add directly;

可以直接添加;

array_list.Add(temp_list);

回答by CloudyMarble

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

while (dr.Read())
{
   string one = dr["Row1"].ToString();
   string two = dr["Row2"].ToString();
   List<string> temp_list = new List<string> { one, two };
   array_list.add(temp_list)
}

回答by RTRokzzz

List<List<string>> array_list = new List<List<string>>();
while (dr.Read())
        {
            string one = dr["Row1"].ToString();
            string two = dr["Row2"].ToString();

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

            array_list.Add(temp_list);
        }

回答by Miko?aj Frankiewicz

You have to alweys remeber to make new temp_list, don't use temp_list.clear(), like I did in my project =_=.

您必须始终记住创建新的 temp_list,不要使用 temp_list.clear(),就像我在我的项目 =_= 中所做的那样。

Blockquote

块引用

 List<List<string>> array_list = new List<List<string>>();
    while (dr.Read())
            {
                string one = dr["Row1"].ToString();
                string two = dr["Row2"].ToString();

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

                array_list.Add(temp_list);
            }

Blockquote

块引用