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.
- Using a Loop.
- Using LINQ.
- Using a Generic Method.
For this example I am creating a simple Student class like:
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public string Address { get; set; }
public string MobileNo { get; set; }
}
And a DataTable with some data like:
DataTable dt = new DataTable("Student");
dt.Columns.Add("StudentId", typeof(Int32));
dt.Columns.Add("StudentName", typeof(string));
dt.Columns.Add("Address", typeof(string));
dt.Columns.Add("MobileNo", typeof(string));
//Data
dt.Rows.Add(1, "Manish", "Hyderabad","0000000000");
dt.Rows.Add(2, "Venkat", "Hyderabad", "111111111");
dt.Rows.Add(3, "Namit", "Pune", "1222222222");
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.
public void StudentList()
{
// DataTable dt = new DataTable("Branches");
DataTable dt = new DataTable("Student");
dt.Columns.Add("StudentId", typeof(Int32));
dt.Columns.Add("StudentName", typeof(string));
dt.Columns.Add("Address", typeof(string));
dt.Columns.Add("MobileNo", typeof(string));
//Data
dt.Rows.Add(1, "Manish", "Hyderabad", "0000000000");
dt.Rows.Add(2, "Venkat", "Hyderabad", "111111111");
dt.Rows.Add(3, "Namit", "Pune", "1222222222");
dt.Rows.Add(4, "Abhinav", "Bhagalpur", "3333333333");
List<Student> studentList = new List<Student>();
for (int i = 0; i < dt.Rows.Count; i++)
{
Student student = new Student();
student.StudentId = Convert .ToInt32 (dt.Rows[i]["StudentId"]);
student.StudentName = dt.Rows[i]["StudentName"].ToString();
student.Address = dt.Rows[i]["Address"].ToString();
student.MobileNo = dt.Rows[i]["MobileNo"].ToString();
studentList.Add(student);
}
}
Convert DataTable to List Using Linq
This is the modern approach for creating a List in C#.
public void StudentListUsingLink()
{
// DataTable dt = new DataTable("Branches");
DataTable dt = new DataTable("Student");
dt.Columns.Add("StudentId", typeof(Int32));
dt.Columns.Add("StudentName", typeof(string));
dt.Columns.Add("Address", typeof(string));
dt.Columns.Add("MobileNo", typeof(string));
//Data
dt.Rows.Add(1, "Manish", "Hyderabad", "0000000000");
dt.Rows.Add(2, "Venkat", "Hyderabad", "111111111");
dt.Rows.Add(3, "Namit", "Pune", "1222222222");
dt.Rows.Add(4, "Abhinav", "Bhagalpur", "3333333333");
List<Student> studentList = new List<Student>();
studentList = (from DataRow dr in dt.Rows
select new Student()
{
StudentId = Convert .ToInt32 (dr["StudentId"]),
StudentName = dr["StudentName"].ToString(),
Address = dr["Address"].ToString(),
MobileNo = dr["MobileNo"].ToString()
}).ToList();
}
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.
private static List<T> ConvertDataTable<T>(DataTable dt)
{
List<T> data = new List<T>();
foreach (DataRow row in dt.Rows)
{
T item = GetItem<T>(row);
data.Add(item);
}
return data;
}
private static T GetItem<T>(DataRow dr)
{
Type temp = typeof(T);
T obj = Activator.CreateInstance<T>();
foreach (DataColumn column in dr.Table.Columns)
{
foreach (PropertyInfo pro in temp.GetProperties())
{
if (pro.Name == column.ColumnName)
pro.SetValue(obj, dr[column.ColumnName], null);
else
continue;
}
}
return obj;
}
To call the preceding method, use the following syntax:
List< Student > studentDetails = new List< Student >();
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.

Maximiliano CalderonPosted Aug 30, 2022, 3:51 PM
I changed the "GetItem<T>(DataRow dr)" method to something like this private static T GetItem<T>(DataRow dr) { Type temp = typeof(T); T obj = Activator.CreateInstance<T>(); List<PropertyInfo> props = temp.GetProperties().ToList(); foreach (DataColumn column in dr.Table.Columns) { var pro = props.FirstOrDefault(x => x.Name == column.ColumnName); if (pro != null) pro.SetValue(obj, dr[column.ColumnName].GetType() == typeof(DBNull) ? null : dr[column.ColumnName], null); } return obj; }
Paul NeibargerPosted Aug 31, 2021, 1:25 PM
"Note: The advantage of the preceding two method is we can something." - under Convert DataTable to List Using Linq heading. This statement leaves me with a question. What does this mean?
suresh mPosted Apr 9, 2019, 3:39 AM
Does any performance slow issue occur using ConvertDataTable generic method?
anant bhattPosted Aug 30, 2018, 5:14 AM
Please tell me konw who method is best for Large Data column and Large Data Row.
NIKHIL VARTAKPosted May 29, 2018, 10:20 AM
It is better to avoid looping through type properties for every column in in a row. Instead of "foreach (PropertyInfo pro in temp.GetProperties())" inside loop we can do "PropertyInfo[] properties = type.GetProperties();" outside of loop and then "var prop = properties.FirstOrDefault(x => x.Name == column.ColumnName);" inside the loop.
Ravi PatelPosted Sep 22, 2017, 3:30 AM
Nice explanation thanks
Thakur DasPosted Aug 5, 2017, 3:08 AM
Nice article manish it helps me a lot
Garrett JohnsonPosted Aug 1, 2017, 4:40 PM
When I run the generic method I am getting.. "No parameterless constructor defined for this object." It comes from the Activator.CreateInstance line. Any ideas?
Samuel IyomerePosted Dec 16, 2015, 4:17 AM
But the third pattern you showed here will seem to solve my above problem. Thanks for sharing. this should save me a bunch of time and coding.
Samuel IyomerePosted Dec 16, 2015, 4:15 AM
Nice one, I've used the loop before (foreach DaraRow dr in dt[0].Rows) which seems to work very well but quite annoying when you have to repeat it for several classes from several DataTable with different structure.
vineeth mvPosted Dec 15, 2015, 1:25 AM
Best way to use dapper class. We can avoid loops..
Darshit shahPosted Jun 24, 2015, 2:20 PM
in thti get db.null value error
Manish Kumar ChoudharyPosted Nov 27, 2014, 3:29 AM
Thanks Daniel Hopkins sir..
Daniel HopkinsPosted Nov 27, 2014, 3:26 AM
Entity framework makes this so much easier. Thanks for the article though.
Manish Kumar ChoudharyPosted Nov 25, 2014, 10:48 PM
Thanks Del Woodcock sir..
Del WoodcockPosted Nov 25, 2014, 10:40 AM
Great article
Manish Kumar ChoudharyPosted Nov 25, 2014, 3:59 AM
Thanks Vithal Wadje..
Vithal WadjePosted Nov 25, 2014, 3:55 AM
good one