[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How do I get multiple column values from data table.

Posted on 2016-09-21
4
Medium Priority
?
67 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 45

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 53

Assisted Solution

by:Ryan Chong
Ryan Chong earned 400 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 10

Accepted Solution

by:
Paweł earned 1600 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 44

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

650 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