简体   繁体   English

如何将强类型数据表转换为无类型数据表

[英]How do I convert a strongly typed DataTable to an untyped DataTable

I tried to send a strongly typed DataTable to a WCF service that was expecting an untyped DataTable with the same data. 我试图将强类型的DataTable发送到WCF服务,该服务期望具有相同数据的无类型的DataTable。

However, I got an error because the serializer didn't know what to do with the extra properties. 但是,我出错了,因为序列化器不知道如何处理这些额外的属性。

How can I convert my strongly typed DataTable to its untyped equivalent? 如何将我的强类型数据表转换为其等效的无类型数据?

Note: For a basic explanation of Strongly Typed vs. Untyped DataSets, look here . 注意:有关强类型数据集和无类型数据集的基本说明, 参见此处

This can be done with a simple merge command, like this: 这可以通过简单的合并命令来完成,如下所示:

// convert stringly typed DataTable "data" to a loosely-typed version of itself
var data_untyped = new DataTable(data.TableName, data.Namespace);
data_untyped.Merge(data);

The first (non-comment) line creates the new DataTable, copying the TableName and Namespace. 第一行(非注释行)创建新的DataTable,复制TableName和Namespace。

The last line copies the data, including the column definitions. 最后一行复制数据,包括列定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM