c# - Transfer specific datatable contents to another array -


i'm trying transfer datatable contents array. going specific fields datatable, not know how save specific columns content array.

what did:

        dr = superviewbll.getsomestuff();         string[] new_array;          if (dr.rows.count > 0)         {              (int = 0; < dr.rows.count;i++ )             {                new_array[i] = dr.rows[i]['stufflocationid'];                // know wrong             }         } 

how can column stufflocationid array new_array?

that line right, except need double quotes rather single , need cast/convert value type string put string array:

new_array[i] = (string) dr.rows[i]["stufflocationid"]; 

or, if data nullable:

new_array[i] = dr.rows[i]["stufflocationid"] string; 

the issue array doesn't exist because haven't created it. in order create array have know size. if haven't specified size, either implicitly or explicitly, haven't created array. this:

string[] new_array; 

should this:

string[] new_array = new string[dr.rows.count - 1]; 

you can throw bit of linq @ problem , succinctify code bit:

var new_array = dr.asenumerable()                   .select(row => row["stufflocationid"] string)                   .toarray(); 

that's example of specifying size of array implicitly.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -