将模型导出到数据表 [英] Export Model to DataTable

查看:38
本文介绍了将模型导出到数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的模型数据转换为 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();

这篇关于将模型导出到数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆