C# 将通用数据表转换为类型化数据表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1398609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:04:10  来源:igfitidea点击:

Casting generic datatable to typed datatable

c#datatablestrongly-typed-dataset

提问by callisto

I need to reuse a DataAccess method prescribed by client. This method returns a vanilla datatable. I want to cast this datatable to my Typed datatable. The amount of columns and their types will match. The exception message "Unable to cast object of type 'System.Data.DataTable' to type 'MarketValueDataTable'." is very clear, but how do I fix it?

我需要重用客户端规定的 DataAccess 方法。此方法返回一个普通的数据表。我想将此数据表转换为我的 Typed 数据表。列的数量及其类型将匹配。异常消息“无法将‘System.Data.DataTable’类型的对象转换为‘MarketValueDataTable’类型。” 很清楚,但我该如何解决?

Had a look at casting-a-base-type-to-a-derived-type, but could not see how to make that work.

看了一下cast-a-base-type-to-a-derived-type,但不知道如何使它起作用。

I cannot populate the datatable with a datareader, can only use the client's DataAccess method.

我无法使用数据读取器填充数据表,只能使用客户端的 DataAccess 方法。

采纳答案by Thomas Levesque

The cast could only work if the table returned by the method was actually an instance of MarketValueDataTable. If it's not, all you can do is copy the data to an instance of MarketValueDataTable. You could for instance use the Mergemethod :

只有当方法返回的表实际上是MarketValueDataTable. 如果不是,您所能做的就是将数据复制到MarketValueDataTable. 例如,您可以使用该Merge方法:

DataTable data = GetData();
MarketValueDataTable myData = new MarketValueDataTable();
myData.Merge(data);

回答by Tor Haugen

You cannot cast the DataTable to a MarketValueDataTable, for the simple reason that it doesn't inherit from it.

您不能将 DataTable 转换为 MarketValueDataTable,原因很简单,它不继承自它。

What you need to do is instantiate a new MarketValueDataTable, then copy the data from the DataTable into it.

您需要做的是实例化一个新的 MarketValueDataTable,然后将 DataTable 中的数据复制到其中。

Try something like:

尝试类似:

MarketValueDataTable myTable = new MarketValueDataTable();
StringWriter writer = new StringWriter();
theDataTable.WriteXml(writer);
myTable.ReadXml(new StringReader(writer.ToString()));

(I haven't tested this)

(这个我没测试过)

回答by Rune Grimstad

If your datatable is a generic table then it can't be cast to a MarketValueDataTable.

如果您的数据表是通用表,则无法将其转换为 MarketValueDataTable。

One thing you can try is to create a new MarketValueDataTable object and manually add rows to it. By stepping through the rows of your generic datatable and then stepping through the columns of each row you could use reflection to set the property values of a new MarketValueDataTableRow.

您可以尝试的一件事是创建一个新的 MarketValueDataTable 对象并手动向其中添加行。通过逐步浏览通用数据表的行,然后逐步浏览每行的列,您可以使用反射来设置新 MarketValueDataTableRow 的属性值。

Something like the following (warning - pseudocode):

类似于以下内容(警告 - 伪代码):

MarketValueDataTable mv = new MarketValueDataTable();
foreach(DataRow row in table.Rows)
{

   MarketValueDataTableRow mvrow = mv.NewRow();
   foreach(DataColumn col in table.Columns)
   {
      PropertyInfo colProperty = mvrow.GetType().GetProperty(col.Name);
      colProperty.SetValue(mvRow, row[col]);
   }
   mv.Rows.Add(mvrow);

}

You get the general idea. Step through the rows of the generic table and use reflection to find the matching properties in the specific typed data row. This should work (I have used the same approach earlier), but the code must be modified to match your requirements.

你得到了一般的想法。逐步遍历通用表的行并使用反射在特定类型化数据行中查找匹配的属性。这应该可行(我之前使用过相同的方法),但必须修改代码以符合您的要求。

回答by Carter Medlin

Use this handy function to convert a regular DataTableto a typed DataTable.

使用这个方便的函数将常规转换DataTable为类型化的DataTable.

VB

VB

Public Shared Function ConvertToTypedDataTable(Of T As {Data.DataTable, New})(dtBase As Data.DataTable) As T
    Dim dtTyped As New T
    dtTyped.Merge(dtBase)

    Return dtTyped
End Function

Usage

用法

Dim dtTyped = ConvertToTypedDataTable(Of dataset1.MyTypedDataTable)(MyUntypedDataTable)

C#

C#

public static T ConvertToTypedDataTable<T>(System.Data.DataTable dtBase) where T : Data.DataTable, new()
{
    T dtTyped = new T();
    dtTyped.Merge(dtBase);

    return dtTyped;
}

Usage

用法

dataset1.MyTypedDataTable dtTyped = ConvertToTypedDataTable<dataset1.MyTypedDataTable>(MyUntypedDataTable);