Link to home
Create AccountLog in
Avatar of Isabell
Isabell

asked on

How do I get multiple column values from data table.

Hi guys,

I have Person table and
dt has this table values.

 DataTable dt = getPersonTable();

User generated image
I want to have FirstName and LastName for PersonId = 4 from DataTable, dt.

How can I accomplish this?
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

see https://msdn.microsoft.com/en-us/library/system.data.datatable.rows(v=vs.110).aspx

something like
PersonID = dt.Rows(0).Columns(0)
LastName = dt.Rows(0).Columns(1)
....
SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Here is how I would approach it:
//select record(s) to data rows
DataRow[] childRow = dt.Select("PersonId = 4");
//if at least one record is found
if(childRow.Any())
{
    //copy rows to a datatable
    DataTable childDt = childRows.CopyToDataTable();
}

Open in new window



or, if you just want the First and Last name, you can do:

var test = from row in dt.AsEnumerable()
                    where row.Field<int>("PersonId") == 4
                    select new {First = row.Field<string>("FirstName"), Last = row.Field<string>("LastName")};

Open in new window