Saurabh Saurabh -4 years ago 241
C# Question

Export Model to DataTable

I want to convert my Model data to

DataSet
or
DataTable
(to export in excel format)

db.EMPs.ToList()


db
is
DataContext
,
EMPs
is
dataclass
.


How to export this list to
DataTable
, I can export rows to excel but how to access column name from header (column name should not be added manually in
DataTable
)

Answer Source

You can use ToDataTable extension method but you need to install MoreLinq first. To install MoreLINQ, run the following command in the Package Manager Console:

PM> Install-Package morelinq

Then add the following line to your using directives:

using MoreLinq;

And finally you can use ToDataTable extension method:

DataTable s = db.EMPs.ToDataTable();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download