code block for converting a generic list of custom class object to data table.

public DataTable ToDataTable<T>(this List<T> list)
{
    var table = new DataTable();

    var properties = TypeDescriptor.GetProperties(typeof(T));
    foreach (PropertyDescriptor prop in properties)
    {
        table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
    }

    foreach (var item in list)
    {
        var row = table.NewRow();
        foreach (PropertyDescriptor prop in properties)
        {
            row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
        }

        table.Rows.Add(row);
    }

    return table;
}
Discussion
4 X 8 =
** To prevent abusing comments from publishing, posted comments will be reviewed and then published!
 Mritunjay Kumar
Works at Mindfire Solutions

I mostly work with C#, ASP.NET, MVC, WCF, Web API, Entity FrameWork, MS Sql.

More under this category...
Convert data table to generic list
Extension method to convert a delimited string to list of generic primitive type
Generate random string from set of fixed chars
Google cloud storage API - Rename a file
Initializing X509Certificate2, Windows 2012 + IIS8 + .Net 4.5
Google cloud storage API - upload file with progress bar
Google cloud storage API - download file with progress bar
All under this category...