Solved

How do I get multiple column values from data table.

Posted on 2016-09-21
4
51 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 (Megan)
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

861 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

22 Experts available now in Live!

Get 1:1 Help Now