Click here to Skip to main content
13,146,014 members (42,504 online)
Click here to Skip to main content


43 bookmarked
Posted 3 Mar 2009

Tips for LINQ: Fill any generic List from a DataTable

, 3 Mar 2009
This article shows how to populate any generic list from a DataTable.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
//This is neccesary in order to "navigate" throught the class members
using System.Reflection; 

namespace FromDataTableToGenericListExample
    //In order to return any generic container (in this example is a list)
    //,the class must be generic
    public class DataFiller<T>
        public List<T> FromDataTableToList(DataTable dataTable)
            //This create a new list with the same type of the generic class
            List<T> genericList = new List<T>();

            //Obtains the type of the generic class
            Type t = typeof(T);
            //Obtains the properties definition of the generic class.
            //With this, we are going to know the property names of the class
            PropertyInfo[] pi = t.GetProperties();

            //For each row in the datatable
            foreach (DataRow row in dataTable.Rows)
                //Create a new instance of the generic class
                object defaultInstance = Activator.CreateInstance(t);

                //For each property in the properties of the class
                foreach (PropertyInfo prop in pi)
                        //Get the value of the row according to the field name
                        //Remember that the class´s members and the table´s field names
                        //must be identical
                        object columnvalue = row[prop.Name];

                        //Know check if the value is null. If not, it will be added to the instance
                        if (columnvalue!= DBNull.Value)
                            //Set the value dinamically. Now you need to pass as an argument
                            //an instance class of the generic class. This instance has been
                            //created with Activator.CreateInstance(t)
                            prop.SetValue(defaultInstance, columnvalue, null);
                    catch (Exception ex)
                        Console.WriteLine(prop.Name + ": " + ex.ToString());
                        return null;

                //Now, create a class of the same type of the generic class. 
                //Then a conversion it´s done to set the value
                T myclass = (T)defaultInstance;
                //Add the generic instance to the generic list


            //At this moment, the generic list contains all de datatable values
            return genericList;

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


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


About the Author

Santiago Sanchez
Software Developer
Spain Spain
I´ve been working with Oracle, Sql Server and Visual Basic 6 since 2003 and with C# since 2006. Now I´m fighting with Biztalk 2006 too...

MCTS - .NET Framework 4, Windows Applications
MCTS - Accessing Data with .NET Framework 4

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 3 Mar 2009
Article Copyright 2009 by Santiago Sanchez
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid