Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Tips for LINQ: Fill any generic List from a DataTable

, 3 Mar 2009 CPOL
This article shows how to populate any generic list from a DataTable.
FromDataTableToGenericListExample.zip
FromDataTableToGenericListExample
bin
Release
FromDataTableToGenericListExample.exe
FromDataTableToGenericListExample.vshost.exe
FromDataTableToGenericListExample.vshost.exe.manifest
Data
Database
FromDataTableToGenericListExample.csproj.user
Process
Properties
Scripts
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)
                {
                    try
                    {
                        //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
                genericList.Add(myclass);

            }

            //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.

License

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

Share

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

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