[英]C# Add List<string> to List<List<string>> array
我能夠以這種方式在List<List<string>>
數組中添加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);
這應該工作:
array_list.Add(temp_list);
更改變量聲明以初始化空List:
List<List<string>> array_list = new List<List<string>>();
然后,只需調用.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);
除非我讀錯了,否則你應該做到:
array_list.add(temp_list);
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)
}
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);
}
你可以直接添加;
array_list.Add(temp_list);
你必須記住制作新的temp_list,不要使用temp_list.clear(),就像我在我的項目中所做的那樣= _ =。
大段引用
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);
}
大段引用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.