Solved

How do I get multiple column values from data table.

Posted on 2016-09-21
4
47 Views
Last Modified: 2016-09-21
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?
0
Comment
Question by:Isabell
4 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41808406
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)
....
0
 
LVL 49

Assisted Solution

by:Ryan Chong
Ryan Chong earned 100 total points
ID: 41808413
you can also try:

DataRow[] dr = dt.Select("PersonId = '4' ")

Open in new window


then refer to DataRow collection instead.
0
 
LVL 8

Accepted Solution

by:
Paweł earned 400 total points
ID: 41808418
you can also use linq

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication4
{
    class Program
    {
        static DataTable dt = new DataTable();
        static void Main(string[] args)
        {
            dt.Columns.Add(new DataColumn("Id", typeof(int)));
            dt.Columns.Add(new DataColumn("LastName", typeof(string)));
            dt.Columns.Add(new DataColumn("FirstName", typeof(string)));
            dt.Columns.Add(new DataColumn("BirthDate", typeof(DateTime)));

            NewRow(1, "paul", "chooch", new DateTime(1984, 1, 31));
            NewRow(2, "tom", "chooch", new DateTime(1988, 8, 28));
            NewRow(3, "john", "smith", new DateTime(1963, 7, 14));
            NewRow(4, "jane", "doe", new DateTime(1992, 4, 13));

            var output = dt.AsEnumerable()
                .Where(dr => dr.Field<int>("Id") == 2)
                .Select(dr => new
                {
                    fName = dr.Field<string>("FirstName"),
                    lName = dr.Field<string>("LastName")
                  
                    
                }).FirstOrDefault();

            Console.WriteLine( output.fName + " " + output.lName);

        // or combine mine and Ryan's approach
        var output2 = dt.Select("Id = 2")
                .Select(dr => dr.Field<string>("FirstName") + " " + dr.Field<string>("LastName") ).First();

            Console.WriteLine(output2);
        }

        static void NewRow(int id, string lName, string fName, DateTime bDate)
        {
            var dr = dt.NewRow();
            dr.SetField("Id", id);
            dr.SetField("LastName", lName);
            dr.SetField("FirstName", fName);
            dr.SetField("BirthDate", bDate);
            dt.Rows.Add(dr);
        }
    }
}

Open in new window

0
 
LVL 42

Expert Comment

by:zephyr_hex
ID: 41809056
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

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now