In Focus

Convert DataTable to List In C#

This article shows 3 ways to convert a DataTable to a List in C#.

Introduction

This article explains various ways to convert a DataTable to a List in C#. There are the following 3 ways to convert a DataTable to a List.

  1. Using a Loop.
  2. Using LINQ.
  3. Using a Generic Method.

For this example I am creating a simple Student class like:

  1. public class Student  
  2. {  
  3.     public int StudentId { getset; }  
  4.     public string StudentName { getset; }  
  5.     public string Address { getset; }  
  6.     public string MobileNo { getset; }  

And a DataTable with some data like:

  1. DataTable dt = new DataTable("Student");  
  2. dt.Columns.Add("StudentId"typeof(Int32));  
  3. dt.Columns.Add("StudentName"typeof(string));  
  4. dt.Columns.Add("Address"typeof(string));  
  5. dt.Columns.Add("MobileNo"typeof(string));  
  6.     //Data  
  7. dt.Rows.Add(1, "Manish""Hyderabad","0000000000");  
  8. dt.Rows.Add(2, "Venkat""Hyderabad""111111111");  
  9. dt.Rows.Add(3, "Namit""Pune""1222222222");  
  10. dt.Rows.Add(4, "Abhinav""Bhagalpur""3333333333"); 
Now I will convert the receding DataTable into a List< Student > using all the preceding three methods.

Convert DataTable to List Using a Loop


In this method I am using a simple for loop; other loops can also be used.
  1. public void StudentList()  
  2. {  
  3.     //  DataTable dt = new DataTable("Branches");  
  4.     DataTable dt = new DataTable("Student");  
  5.     dt.Columns.Add("StudentId"typeof(Int32));  
  6.     dt.Columns.Add("StudentName"typeof(string));  
  7.     dt.Columns.Add("Address"typeof(string));  
  8.     dt.Columns.Add("MobileNo"typeof(string));  
  9.     //Data  
  10.     dt.Rows.Add(1, "Manish""Hyderabad""0000000000");  
  11.     dt.Rows.Add(2, "Venkat""Hyderabad""111111111");  
  12.     dt.Rows.Add(3, "Namit""Pune""1222222222");  
  13.     dt.Rows.Add(4, "Abhinav""Bhagalpur""3333333333");  
  14.   
  15.     List<Student> studentList = new List<Student>();  
  16.     for (int i = 0; i < dt.Rows.Count; i++)  
  17.     {  
  18.         Student student = new Student();  
  19.         student.StudentId = Convert .ToInt32 (dt.Rows[i]["StudentId"]);  
  20.         student.StudentName = dt.Rows[i]["StudentName"].ToString();  
  21.         student.Address = dt.Rows[i]["Address"].ToString();  
  22.         student.MobileNo = dt.Rows[i]["MobileNo"].ToString();  
  23.         studentList.Add(student);  
  24.     }  

Convert DataTable to List Using Linq


This is the modern approach for creating a List in C#.
  1. public void StudentListUsingLink()  
  2. {  
  3.     //  DataTable dt = new DataTable("Branches");  
  4.     DataTable dt = new DataTable("Student");  
  5.     dt.Columns.Add("StudentId"typeof(Int32));  
  6.     dt.Columns.Add("StudentName"typeof(string));  
  7.     dt.Columns.Add("Address"typeof(string));  
  8.     dt.Columns.Add("MobileNo"typeof(string));  
  9.     //Data  
  10.     dt.Rows.Add(1, "Manish""Hyderabad""0000000000");  
  11.     dt.Rows.Add(2, "Venkat""Hyderabad""111111111");  
  12.     dt.Rows.Add(3, "Namit""Pune""1222222222");  
  13.     dt.Rows.Add(4, "Abhinav""Bhagalpur""3333333333");  
  14.     List<Student> studentList = new List<Student>();  
  15.     studentList = (from DataRow dr in dt.Rows  
  16.             select new Student()  
  17.             {  
  18.                 StudentId = Convert .ToInt32 (dr["StudentId"]),  
  19.                 StudentName = dr["StudentName"].ToString(),  
  20.                 Address = dr["Address"].ToString(),  
  21.                 MobileNo = dr["MobileNo"].ToString()  
  22.             }).ToList();  
  23.      

Note: The advantage of the preceding two method is we can something.

Convert DataTable to List using a Generic Method


This is a generic method that will convert any type of DataTable to a List (the DataTable structure and List class structure should be the same).

The following are the two functions in which if we pass a DataTable and a user defined class. It will then return the List of that class with the DataTable data. 
  1. private static List<T> ConvertDataTable<T>(DataTable dt)  
  2. {  
  3.     List<T> data = new List<T>();  
  4.     foreach (DataRow row in dt.Rows)  
  5.     {  
  6.         T item = GetItem<T>(row);  
  7.         data.Add(item);  
  8.     }  
  9.     return data;  
  10. }  
  11. private static T GetItem<T>(DataRow dr)  
  12. {  
  13.     Type temp = typeof(T);  
  14.     T obj = Activator.CreateInstance<T>();  
  15.   
  16.     foreach (DataColumn column in dr.Table.Columns)  
  17.     {  
  18.         foreach (PropertyInfo pro in temp.GetProperties())  
  19.         {  
  20.             if (pro.Name == column.ColumnName)  
  21.                 pro.SetValue(obj, dr[column.ColumnName], null);  
  22.             else  
  23.                 continue;  
  24.         }  
  25.     }  
  26.     return obj;  

To call the preceding method, use the following syntax:

  1. List< Student > studentDetails = new List< Student >();  
  2. studentDetails = ConvertDataTable< Student >(dt); 
Change the Student class name and dt value based on your requirements. In this case the DataTable column's name and class property name should be the same otherwise this function will not work properly.

Download the source code for clarification.

Summary


In this illustration you came to understand the various way to convert a DataTable to a List.