C-Sharp Convert DataTable Object into List Object

In this article we will convert an DataTable Object into List Object. and learn c# datatable, c# list and generic collection in c#. Sometimes we will face a situation in mixed mode code standard like Entity Framework (EF) with Ado.net. For such code standards we needs to convert DataTable object into List Object and vice versa.

Step 1: Open Visual Studio. Create Console Application. Import required namesapces. System.Collections for ArrayList, System.Collections.Generic for List, System for Console and System.Data for DataTable.

using System; using System.Collections; using System.Collections.Generic; using System.Data;

Step 2: Create an DataTable with Columns related to Student and populate DataTable with some dummy student data.

DataTable dt = new DataTable("Students"); //Creates DataTable Object DataTable dt = new DataTable("Students"); //Create Static Columns for DataTable dt.Columns.Add("StudentId", Type.GetType("System.Int32")); dt.Columns.Add("RollNo", Type.GetType("System.Int32")); dt.Columns.Add("ClassName"); dt.Columns.Add("Sectionname"); dt.Columns.Add("SessionName"); dt.Columns.Add("StudentName"); ArrayList n = new ArrayList(); n.Add("Jack"); n.Add("Maria"); n.Add("Wiliam"); n.Add("Rock"); n.Add("Marina"); n.Add("Harish"); n.Add("Jhon"); n.Add("Raidu"); int i = 1; foreach (string s in n) { dt.Rows.Add(i, i + 1000, "10th", "C", "2019-2020", s); i++; } //Retrive Data From DataTable foreach (DataRow r in dt.Rows) { Console.WriteLine("========================================="); foreach (DataColumn c in dt.Columns) { Console.WriteLine($"{c.ToString()}: {r[c.ToString()]}"); } }

Step 3: Create an Data Transfer Object Class for Student and map its properties with Student DataTable.

public class StudentDTO { public int StudentId { get; set; } public int RollNo { get; set; } public string ClassName { get; set; } public string Sectionname { get; set; } public string SessionName { get; set; } public string StudentName { get; set; } }

Step 4: Create List Object for StudetDTO and insert data from DataTable.

// Creating Student DTO Object List stu = new List(); foreach (DataRow r in dt.Rows) { stu.Add(new StudentDTO() { StudentId = Convert.ToInt32(r["StudentId"]), RollNo = Convert.ToInt32(r["RollNo"]), ClassName = Convert.ToString(r["ClassName"]), Sectionname = Convert.ToString(r["Sectionname"]), SessionName = Convert.ToString(r["SessionName"]), StudentName = Convert.ToString(r["StudentName"]), }); }

Step 5: Retrieve Student data from List Generic object.

foreach (StudentDTO s in stu) { Console.WriteLine($"StudentId: {s.StudentId} StudentName: {s.StudentName}"); }

The overall program will be as follows.

using System; using System.Collections; using System.Collections.Generic; using System.Data; namespace ConsoleApplication { public class StudentDTO { public int StudentId { get; set; } public int RollNo { get; set; } public string ClassName { get; set; } public string Sectionname { get; set; } public string SessionName { get; set; } public string StudentName { get; set; } } class Program { static void Main(string[] args) { //Creates DataTable Object DataTable dt = new DataTable("Students"); //Create Static Columns for DataTable dt.Columns.Add("StudentId", Type.GetType("System.Int32")); dt.Columns.Add("RollNo", Type.GetType("System.Int32")); dt.Columns.Add("ClassName"); dt.Columns.Add("Sectionname"); dt.Columns.Add("SessionName"); dt.Columns.Add("StudentName"); ArrayList n = new ArrayList(); n.Add("Jack"); n.Add("Maria"); n.Add("Wiliam"); n.Add("Rock"); n.Add("Marina"); n.Add("Harish"); n.Add("Jhon"); n.Add("Raidu"); int i = 1; foreach (string s in n) { dt.Rows.Add(i, i + 1000, "10th", "C", "2019-2020", s); i++; } //Retrive Data From DataTable foreach (DataRow r in dt.Rows) { Console.WriteLine("========================================="); foreach (DataColumn c in dt.Columns) { Console.WriteLine($"{c.ToString()}: {r[c.ToString()]}"); } } // Creating Student DTO Object List stu = new List(); foreach (DataRow r in dt.Rows) { stu.Add(new StudentDTO() { StudentId = Convert.ToInt32(r["StudentId"]), RollNo = Convert.ToInt32(r["RollNo"]), ClassName = Convert.ToString(r["ClassName"]), Sectionname = Convert.ToString(r["Sectionname"]), SessionName = Convert.ToString(r["SessionName"]), StudentName = Convert.ToString(r["StudentName"]), }); } Console.WriteLine("=========List Generic Data:=============="); //Retrive Data From List Generic foreach (StudentDTO s in stu) { Console.WriteLine($"StudentId: {s.StudentId} StudentName: {s.StudentName}"); } Console.ReadKey(); } } }

If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.


Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.

 
 

{{c.Content}}

Comment By: {{c.Author}}  On:   {{c.CreatedDate|date:'dd/MM/yyyy'}} / Reply


Categories