将模型导出到DataTable
我想将我的模型数据转换为 DataSet
或 DataTable
(以Excel格式导出)
I want to convert my Model data to DataSet
or DataTable
(to export in excel format)
db.EMPs.ToList()
db
是 DataContext
, EMPs
是 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 > install-Package morelinq
PM> Install-Package morelinq
然后使用指令将以下行添加到您的 :
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();