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();

Person table
I want to have FirstName and LastName for PersonId = 4 from DataTable, dt.

How can I accomplish this?
C#ASP.NET.NET Programming

Avatar of undefined
Last Comment
zephyr_hex (Megan)

8/22/2022 - Mon
AndyAinscow

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
Ryan Chong

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Paweł

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
zephyr_hex (Megan)

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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy