Reputation: 2613
I want to return the value of a DataRow[]
to a string in c#
Here is my DataTable
:
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("BugDescription", typeof(string));
table.Columns.Add("UnitPrice", typeof(double));
table.Rows.Add(1, "Bug 1", 10.00);
table.Rows.Add(2, "Bug 2", 20.00);
I then create a DataRow[]
called result
which stores the row where the ID = 1:
DataRow[] result = table.Select("ID = 1");
The last step that I want to achieve is to add the BugDescription value to a string named description
.
How will I achieve this?
Upvotes: 1
Views: 4250
Reputation: 95
I know I'm pretty late to provide an answer, but, we can add one more to the list of answers.
As the datatable.select is giving us the result in the form of array, it is to know that we are getting itemarray for columns in each row array. Simplifying this statement with below example.
We can use "ItemArray" if we know/remember/use the column position/number rather than the column name
//ID Name Age
//100 Name 100 Age 100
//101 Name 101 Age 101
//102 Name 102 Age 102
Assuming single row.
DataTable dt=new DataTable();
//Assigning some data into dt. with columns ID, Name, Age.
DataRow[] dr=dt.Select("ID=100");
string PersonID=dr[0].ItemArray[0].Tostring().trim(); //first column is ID
string PersonName=dr[0].ItemArray[1].Tostring().trim(); //second column is Name
string PersonAge=dr[0].ItemArray[2].Tostring().trim(); //third column is Age
Therefore Variables will have following details.
// PersonID= 100; PersonName= Name 100; PersonAge= Age 100
Assuming rows>1 (2 in this example)
dr=dt.Select("ID>100");
string PersonID_1=dr[0].ItemArray[0].Tostring().trim(); //first column is ID
string PersonName_1=dr[0].ItemArray[1].Tostring().trim(); //second column is Name
string PersonAge_1=dr[0].ItemArray[2].Tostring().trim(); //third column is Age
string PersonID_2=dr[1].ItemArray[0].Tostring().trim(); //first column is ID
string PersonName_2=dr[1].ItemArray[1].Tostring().trim(); //second column is Name
string PersonAge_2=dr[1].ItemArray[2].Tostring().trim(); //third column is Age
Therefore Variables will have following details.
// PersonID_1= 101; PersonName_1= Name 101; PersonAge_1= Age 101
// PersonID_2= 102; PersonName_2= Name 102; PersonAge_2= Age 102
Point to remember: first row or column index id always starts with 0. Therefore, dr[0] is first row & ItemArray[0] is first column
Upvotes: 1
Reputation: 1430
To achieve your goal you need something like this:
if (result.Length > 0)
{
var description = result[0]["BugDescription"].ToString();
}
Upvotes: 0
Reputation: 740
If you know you're only going to get one row back you can condense the whole thing down to this
string description = table.Select("ID = 1").First().Field<string>("BugDescription");
Upvotes: 0
Reputation: 32701
your code
DataRow[] result = table.Select("ID = 1");
tells you that you have got an array of DataRows. Now it means that you may have more than one record here. So, now it depends on you which Row to assign. You can do it like this if you think it will be the first one
if(result.Length > 0)
{
string description = Convert.ToString(result[0]["BugDescription"]);
}
doing it the linq way
string description = table.Rows.OfType<DataRow>().Where(row => (string)row["ID"] == "1").Select(row => (string)row["BugDescription"]).First();
Upvotes: 2
Reputation: 18411
If you have an array of DataRows as you have since you are declaring it
DataRow[]
You can access it as:
string resultBug = result[0]["BugDescription"];
But since you are only expecting one row (and you are to judge if you will always expect one row to be returned) then you should declare it as a plain DataRow:
DataRow result = table.Select("ID = 1")[0];
string resultBug = result["BugDescription"].Dump();
The Select is returning an array of rows so you should index it with [0] to get the first occurrence.
Upvotes: 0