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 =>
new Category
{
Id = Convert.ToInt64(dr.Field<double>("ID")),
Name = dr.Field<string>("Name"),
DisplayOrder = Convert.ToInt32(dr.Field<double>("Display Order")),
IsActive= dr.Field<double>("Active") == 1 ? true : false
}
).ToList();
IsActive= dr.Field<double>("Active") > 0
(your ternary is unnecessary as the comparison already evaluates totrue
orfalse
) because ofdouble
rounding issues and equality comparisons. \$\endgroup\$dr["ID"]
,dr["Name"]
etc. They're of typeobject
, but theConvert.To____()
functions handle that. \$\endgroup\$