Introduction
In this article, I will describe to you how to convert a List objects to a DataTable.
Step 1: Create a console application and add the Student class with the properties as below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Compare2Objects
{
public class Program
{
static void Main(string[] args)
{
}
}
public class Student
{
public string Name { get; set; }
public int StudentId { get; set; }
public int? Age { get; set; }
}
}
Step 2: In Main method, create a list of students as below.
List<Student> Students = new List<Student>(){
new Student() { Name = "Jack", Age = 15, StudentId = 100 },
new Student() { Name = "Smith", Age = 15, StudentId = 101 },
new Student() { Name = "Smit", Age = 15, StudentId = 102 }
};
Step 3: Now we are going to convert this list object to a DataTable. For that we need to create a new class and a conversion method as below.
public class ListtoDataTableConverter
{
public DataTable ToDataTable<T>(List<T> items)
{
DataTable dataTable = new DataTable(typeof(T).Name);
//Get all the properties
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
//Setting column names as Property names
dataTable.Columns.Add(prop.Name);
}
foreach (T item in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
//inserting property values to datatable rows
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
//put a breakpoint here and check datatable
return dataTable;
}
}
The above method will set the property name as a column name for the DataTable and for each object in the list; it will create a new row in the DataTable and insert values.
Step 4: Now create an instance of the above class and call the method with the list object from the Main method.
ListtoDataTableConverter converter = new ListtoDataTableConverter();
DataTable dt = converter.ToDataTable(Students);
For the complete source code, please find the attached solution.

John VPosted Aug 15, 2024, 3:48 PM
Santosh, This works well if all of your property names exactly match your SQL column names. Quite often they DO NOT. For example, many generated models will take a SQL column name like "SOMETHING_JD" and create an EF property called "SomethingId". And in the context within the modelBuilder for that entity will call something like this: entity.Property(e => e.SomethingId).HasColumnName("SOMETHING_ID"); This creates a mapping from the column to the SQL column name. So you need a way to retrieve that SQL name.
archel archelPosted Jun 16, 2023, 9:25 AM
Thanks very useful! God bless you more
Mahnaz ZamaniPosted Jan 23, 2023, 8:16 AM
Thanks for posting this solution. It was useful for me.
Dominik HaelgPosted Oct 7, 2022, 7:19 AM
I think it should be mentioned that System.Reflection is also required.
John SmithPosted Apr 16, 2019, 3:01 PM
Great example. In my student class I have a property which is a List<Sites>. is it possible to loop through this list as well and add each property as a column during the conversion?
Faezeh ZPosted Nov 10, 2017, 9:54 AM
Thank you so much. It works nicely;
sridhar thotaPosted Jul 14, 2017, 4:26 AM
Excellent stuff. Thank you
Upendra Pratap ShahiPosted Oct 12, 2016, 5:40 AM
Nice one..
Sandeep ChPosted Aug 4, 2016, 2:14 AM
Thanks , i have used this in my project..thanks
Jeeva JPosted Aug 7, 2015, 10:14 AM
Thanks it is very helpful
Bijay SalotryPosted Sep 21, 2013, 5:34 PM
Thanks, it helps a lot.
Carlos AliagaPosted Feb 27, 2013, 12:52 PM
Súper fácil, gracias
henary paulPosted May 14, 2012, 7:23 PM
Thanks,it will be a attractive way to understand about constructing Data Table with the help List Objects.