Reputation: 836
public static List<string>[] ColumnToList2(string table, List<string>[]data, NpgsqlConnection ncon)
{
using (ncon)
{
NpgsqlDataAdapter da = new NpgsqlDataAdapter("SELECT id, name FROM " + table + "", ncon);
DataSet ds = new DataSet();
da.Fill(ds, "" + table + "");
foreach (DataRow row in ds.Tables["" + table + ""].Rows)
{
//Testing area
//Console.WriteLine(row["id"].ToString());
//Console.WriteLine(row["name"].ToString());
data[0].Add(row[0].ToString());
data[1].Add(row[1].ToString());
}
}
return data;
}
Hello again! This time i want to ask advice for searching id from value, then storing both columns in list array. When i test input in console there is what i want, but when try to see after input in list, there isnt the resut i wanted (id name - list) Here goes definition of variables:
List<string>[] data_array = new List<string>[];
datu_array[0]=new List<string>();
datu_array[1]=new List<string>();
When i try to see output through console, there is and error - that i havent defined array size
I hope u can help me solve this problem, thanks in advance.
Upvotes: 0
Views: 3320
Reputation: 630
From what I understood from your question, I would suggest that you use a dictionary instead of a List of string arrays.
Upvotes: 1
Reputation: 460228
I assume that you actually want a List<string[]>
instead of List<string>[]
. So one list and for every row a string[]
with all columns as strings.
You could use LINQ:
List<string[]> data = ds.Tables[0].AsEnumerable()
.Select(row => row.ItemArray
.Select(col => col.ToString())
.ToArray())
.ToList();
But why don't you stay with the DataTable
which has some advantages over the list, for example the columns which you can either access via ordinal index or via name.
Upvotes: 1
Reputation: 25370
Arrays need to be assigned a size when they are instantiated:
List<string>[] data_array = new List<string>[2];
Upvotes: 4