Click here to Skip to main content
Click here to Skip to main content

Conversion Between DataTable and List in C#

, 9 Jun 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Conversion between datatable and List in C#


Sometimes in projects, we may face a situation where we have to convert:

  1. A IList of user defined type to a DataTable or
  2. A DataTable to a List of user defined type

Now rather than making custom conversion mechanism of each different user defined type, let’s make generic converters to save code and time.


Once I faced a situation like:

I had been working on an entity framework based project with itextsharp to make PDF reports. To make reports, itextsharp needed DataTables but entity framework would only give List. So I needed a conversion from List to DataTable.

Another time, I was working with JavaScriptSerializer for json response in web services. But JavaScriptSerializer was unable to serialize DataTables. So I needed conversion from DataTable to List.

List to DataTable

Here is the extension method to convert a List to a DataTable:

/*Converts List To DataTable*/
public static DataTable ToDataTable<TSource>(this IList<TSource> data)
    DataTable dataTable = new DataTable(typeof(TSource).Name);
    PropertyInfo[] props = typeof(TSource).GetProperties(BindingFlags.Public | BindingFlags.Instance);
    foreach (PropertyInfo prop in props)
        dataTable.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? 

    foreach (TSource item in data)
        var values = new object[props.Length];
        for (int i = 0; i < props.Length; i++)
            values[i] = props[i].GetValue(item, null);
    return dataTable;

and the usage would be like, where students is the List<Student>:

List<Student> students = Data.GetStudents();
/*List to DataTable conversion*/
DataTable studentTbl = students.ToDataTable();     

DataTable to List<TSource>

And here is the extension method to convert a DataTable to a List of provided type. Here TSource to indicate which type of list we ask:

/*Converts DataTable To List*/
public static List<TSource> ToList<TSource>(this DataTable dataTable) where TSource : new()
    var dataList = new List<TSource>();

    const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic;
    var objFieldNames = (from PropertyInfo aProp in typeof(TSource).GetProperties(flags)
                            select new { Name = aProp.Name, 
                            Type = Nullable.GetUnderlyingType(aProp.PropertyType) ?? 
                    aProp.PropertyType }).ToList();
    var dataTblFieldNames = (from DataColumn aHeader in dataTable.Columns
                                select new { Name = aHeader.ColumnName, 
                Type = aHeader.DataType }).ToList();
    var commonFields = objFieldNames.Intersect(dataTblFieldNames).ToList();
    foreach (DataRow dataRow in dataTable.AsEnumerable().ToList())
        var aTSource = new TSource();
        foreach (var aField in commonFields)
            PropertyInfo propertyInfos = aTSource.GetType().GetProperty(aField.Name);
            var value = (dataRow[aField.Name] == DBNull.Value) ? 
            null : dataRow[aField.Name]; //if database field is nullable
            propertyInfos.SetValue(aTSource, value, null);
    return dataList;

Let’s say we want to convert a DataTable 'studentTbl' to List<Student>, so the uses of the extension would be like:

/*DataTable to List conversion*/
List<Student> newStudents = studentTbl.ToList<Student>(); 

You may find the project of VS2010 solution in the attachments. If you find any error or problem, just let me know.


  1. It doesn’t work with, has relation instances
  2. Only when the header and type of a DataTables column matches the name and type of a property of a user defined type, DataTable to List conversion takes place.
  3. Works fine with plain objects like:
public class Student
    public long Id { get; set; }
    public string Name { get; set; }
    public short Age { get; set; }
    public DateTime DateOfCreation { get; set; }
    public bool? IsActive { get; set; }


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author


Bangladesh Bangladesh
No Biography provided

Comments and Discussions

Questionissue when converting dataTable to List Pinmemberschazli28-Jan-15 0:08 
GeneralRe: issue when converting dataTable to List PinmemberDiponRoy29-Jan-15 1:33 
AnswerRe: issue when converting dataTable to List PinmemberDiponRoy29-Jan-15 2:54 
QuestionHelpful PinmemberRizwanul Islam1-Jan-15 19:21 
AnswerRe: Helpful PinmemberDiponRoy3-Jan-15 12:07 
QuestionLegend Pinmembermegafam9920-Jun-14 21:05 
AnswerRe: Legend Pinmemberdiponsust22-Jun-14 21:12 
GeneralMy vote of 5 PinprofessionalSunasara Imdadhusen9-Jun-14 23:44 
Generalmicro ORM PinmemberAlexander Sharykin9-Jun-14 21:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150302.1 | Last Updated 9 Jun 2014
Article Copyright 2014 by DiponRoy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid