将模型导出到数据表

问题描述:

我想将我的模型数据转换为 DataSetDataTable(以 excel 格式导出)

I want to convert my Model data to DataSet or DataTable (to export in excel format)

db.EMPs.ToList()

dbDataContextEMPsdataclass .

db is DataContext , EMPs is dataclass .

如何将此列表导出到DataTable,我可以将行导出到excel,但如何从标题中访问列名(列名不应在DataTable 中手动添加)

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)

您可以使用 ToDataTable 扩展方法,但您需要安装 MoreLinq 首先.要安装 MoreLINQ,请在包管理器控制台中运行以下命令:

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> 安装包 morelinq

PM> Install-Package morelinq

然后将以下行添加到您的 using 指令中:

Then add the following line to your using directives:

using MoreLinq;

最后你可以使用 ToDataTable 扩展方法:

And finally you can use ToDataTable extension method:

DataTable s = db.EMPs.ToDataTable();