Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Convert Datatable to Collection using Generic

, 16 Aug 2011
Rate this:
Please Sign up or sign in to vote.
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 to DataTable.
  2. From DataTable to array of T.
// 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)

Share

About the Author

Damian Flynn

United States United States
No Biography provided

Comments and Discussions

 
GeneralReason for my vote of 5 Nice gems :) PinmemberVano Maisuradze16-Aug-11 3:28 

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 | Mobile
Web01 | 2.8.140814.1 | Last Updated 16 Aug 2011
Article Copyright 2011 by Damian Flynn
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid