Converting DataTable to List of class I am using ExcelDataReader to import an Excel file to a dataset.

Example Excel table:


//ID     Name     Display Order    Active
//1      John          1             1



ID, DisplayOrder and Active columns are read as double, so I have to convert them to long, int and bool types respectively. I need to create a list of type Category from the DataTable of the DataSet.

Will this code perform well? Any suggestions for a faster conversion of DataTable to List of class?

var list = result.Tables["Categories"].AsEnumerable()
.Skip(1)
.Select(dr =&gt;
        new Category
            {
                Id = Convert.ToInt64(dr.Field&lt;double&gt;("ID")),
                Name = dr.Field&lt;string&gt;("Name"),
                DisplayOrder = Convert.ToInt32(dr.Field&lt;double&gt;("Display Order")),
                IsActive= dr.Field&lt;double&gt;("Active") == 1 ? true : false
            }
        ).ToList();
I created an extension method for DataTable to convert them into a List&lt;T&gt;

public static class Helper
{
    /// &lt;summary&gt;
    /// Converts a DataTable to a list with generic objects
    /// &lt;/summary&gt;
    /// &lt;typeparam name="T"&gt;Generic object&lt;/typeparam&gt;
    /// &lt;param name="table"&gt;DataTable&lt;/param&gt;
    /// &lt;returns&gt;List with generic objects&lt;/returns&gt;
    public static List&lt;T&gt; DataTableToList&lt;T&gt;(this DataTable table) where T : class, new()
    {
        try
        {
            List&lt;T&gt; list = new List&lt;T&gt;();

            foreach (var row in table.AsEnumerable())
            {
                T obj = new T();

                foreach (var prop in obj.GetType().GetProperties())
                {
                    try
                    {
                        PropertyInfo propertyInfo = obj.GetType().GetProperty(prop.Name);
                        propertyInfo.SetValue(obj, Convert.ChangeType(row[prop.Name], propertyInfo.PropertyType), null);
                    }
                    catch
                    {
                        continue;
                    }
                }

                list.Add(obj);
            }

            return list;
        }
        catch
        {
            return null;
        }
    }
}


Example:

DataTable dtTable = GetEmployeeDataTable();
List&lt;Employee&gt; employeeList = dtTable.DataTableToList&lt;Employee&gt;();