Click here to Skip to main content
11,802,636 members (68,068 online)
Click here to Skip to main content

Conversion Between DataTable and List in C#

, 9 Jun 2014 CPOL 60.6K 1.4K 39
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

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Dayo Okesola6-Aug-15 22:31
memberDayo Okesola6-Aug-15 22:31 
QuestionWorks as expected. Need clarity. Pin
Raja Venkatesh21-Jul-15 22:34
memberRaja Venkatesh21-Jul-15 22:34 
AnswerRe: Works as expected. Need clarity. Pin
DiponRoy22-Jul-15 12:30
memberDiponRoy22-Jul-15 12:30 
QuestionError in Converting List to Datatable Pin
Kalujanaka19-May-15 2:39
memberKalujanaka19-May-15 2:39 
SuggestionRe: Error in Converting List to Datatable Pin
DiponRoy20-May-15 0:17
memberDiponRoy20-May-15 0:17 
QuestionError-DataTable to List conversion Pin
kunal_Vora19-Mar-15 19:29
memberkunal_Vora19-Mar-15 19:29 
AnswerRe: Error-DataTable to List conversion Pin
DiponRoy20-Mar-15 9:19
memberDiponRoy20-Mar-15 9:19 
Questionissue when converting dataTable to List Pin
schazli27-Jan-15 23:08
memberschazli27-Jan-15 23:08 
GeneralRe: issue when converting dataTable to List Pin
DiponRoy29-Jan-15 0:33
memberDiponRoy29-Jan-15 0:33 
AnswerRe: issue when converting dataTable to List Pin
DiponRoy29-Jan-15 1:54
memberDiponRoy29-Jan-15 1:54 
QuestionHelpful Pin
Rizwanul Islam1-Jan-15 18:21
memberRizwanul Islam1-Jan-15 18:21 
AnswerRe: Helpful Pin
DiponRoy3-Jan-15 11:07
memberDiponRoy3-Jan-15 11:07 
QuestionLegend Pin
megafam9920-Jun-14 20:05
membermegafam9920-Jun-14 20:05 
AnswerRe: Legend Pin
diponsust22-Jun-14 20:12
memberdiponsust22-Jun-14 20:12 
GeneralMy vote of 5 Pin
Sunasara Imdadhusen9-Jun-14 22:44
professionalSunasara Imdadhusen9-Jun-14 22:44 
Generalmicro ORM Pin
Alexander Sharykin9-Jun-14 20:51
memberAlexander Sharykin9-Jun-14 20: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
Web03 | 2.8.151002.1 | Last Updated 9 Jun 2014
Article Copyright 2014 by DiponRoy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid