Click here to Skip to main content
15,903,523 members
Articles / Web Development / ASP.NET
Alternative
Tip/Trick

Convert Datatable to Collection using Generic

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
16 Aug 2011CPOL 15.4K   6   3
Here's a couple of gems...From IList to DataTable.From DataTable to array of T.// DataTable: from IListpublic static DataTable ToDataTable(this IList iList){ DataTable dataTable = new DataTable(); PropertyDescriptorCollection propertyDescriptorCollection = ...

Here's a couple of gems...



  1. From IList<t></t> to DataTable.
  2. From DataTable to array of T.

C#
// DataTable: from IList<T>
public static DataTable ToDataTable<T>(this IList<T> iList)
{
    DataTable dataTable = new DataTable();
    PropertyDescriptorCollection propertyDescriptorCollection =
        TypeDescriptor.GetProperties(typeof(T));
    for (int i = 0; i < propertyDescriptorCollection.Count; i++)
    {
        PropertyDescriptor propertyDescriptor = propertyDescriptorCollection[i];
        dataTable.Columns.Add(propertyDescriptor.Name, 
                              propertyDescriptor.PropertyType);
    }
    object[] values = new object[propertyDescriptorCollection.Count];
    foreach (T iListItem in iList)
    {
        for (int i = 0; i < values.Length; i++)
        {
            values[i] = propertyDescriptorCollection[i].GetValue(iListItem);
        }
        dataTable.Rows.Add(values);
    }
    return dataTable;
}

// T[]: from DataTable 
public static T[] ToArray<T>(this DataTable dataTable)
{
    Type tType = typeof(T);
    PropertyInfo[] tPropertiesInfo = tType.GetProperties();
    return ToArray<T>(dataTable, tType, tPropertiesInfo,
        GetColumnIndices(tPropertiesInfo, 
            dataTable.Columns.Cast<DataColumn>().ToArray()));
}

private static int[] GetColumnIndices(PropertyInfo[] tPropertiesInfo, 
                     DataColumn[] dataColumns)
{
    PropertyInfo tPropertyInfo;
    DataColumn dataColumn;

    int[] columnIndicesMappings = new int[tPropertiesInfo.Count()];
    for (int i = 0; i < tPropertiesInfo.Count(); i++)
    {
        tPropertyInfo = tPropertiesInfo[i];
        for (int j = 0; j < dataColumns.Count(); j++)
        {
            dataColumn = dataColumns[j];
            if (tPropertyInfo.Name == dataColumn.ColumnName)
            {
                columnIndicesMappings[i] = j;
                break;
            }
        }
    }
    return columnIndicesMappings;
}

private static T[] ToArray<T>(DataTable dataTable, 
        Type tType, PropertyInfo[] tPropertiesInfo, int[] columnIndices)
{
    DataRow dataRow;
    T tInstance;

    T[] array = new T[dataTable.Rows.Count];
    for (int i = 0; i < dataTable.Rows.Count; i++)
    {
        dataRow = dataTable.Rows[i];
        tInstance = (T)Activator.CreateInstance(tType);
        for (int j = 0; j < tPropertiesInfo.Count(); j++)
        {
            tPropertiesInfo[j].SetValue(tInstance, 
                                 dataRow[columnIndices[j]], null);
        }
        array[i] = tInstance;
    }
    return array;
}

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
TonareSnow22-Jan-13 7:15
TonareSnow22-Jan-13 7:15 
GeneralReason for my vote of 5 Cool Pin
mzukisim22-Aug-11 20:29
mzukisim22-Aug-11 20:29 
GeneralReason for my vote of 5 Nice gems :) Pin
Vano Maisuradze16-Aug-11 3:28
Vano Maisuradze16-Aug-11 3:28 

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

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