vb.net 无法将“System.Data.DataTable”类型的对象转换为“System.Collections.IEnumerable”类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28078760/
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-09-17 18:42:20  来源:igfitidea点击:

Unable to cast object of type 'System.Data.DataTable' to type 'System.Collections.IEnumerable'

vb.netserializationdatatabledataset

提问by Andy

So I created an object called Report. A Report object has several different properties, including a DataSet. This DataSet is supposed to contain at least one DataTable. Report is serializable. In a Unit Test I instantiate a Report object, give its properties values, then serialize it, then deserialize it.

所以我创建了一个名为 Report 的对象。一个 Report 对象有几个不同的属性,包括一个 DataSet。这个DataSet 应该至少包含一个DataTable。报告是可序列化的。在单元测试中,我实例化一个 Report 对象,给出它的属性值,然后序列化它,然后反序列化它。

After adding a method to populate the DataSet, the Deserialization function gives me Error: Unable to cast object of type 'System.Data.DataTable' to type 'System.Collections.IEnumerable'. Shouldn't it be getting serialized as a DataSet...not a DataTable? I'm not sure exactly what is causing me this error. Code snippets are below. Any help is appreciated!

添加填充数据集的方法后,反序列化函数给我错误:无法将类型为“System.Data.DataTable”的对象转换为类型“System.Collections.IEnumerable”。不应该将其序列化为 DataSet...而不是 DataTable?我不确定到底是什么导致了我这个错误。代码片段如下。任何帮助表示赞赏!

<Serializable> Public Class Report

    Private titleStr As String
    Private startDateTime As DateTime
    Private endDateTime As DateTime
    Private numIntervalsShort As Short
    Private binnedDataSet As DataSet

...property gets and sets...namely:

    Property BinnedData As DataSet
        Get
            Return binnedDataSet
        End Get
        Set(ByVal value As DataSet)
            binnedDataSet = value
        End Set
    End Property

End Class


<TestClass()> Public Class ReportObjectTest

    <TestMethod()> Public Sub TestCreateReport()

        Dim testReport As New Project.Report

        testReport.Title = "Test Title"
        testReport.StartDate = "1/1/2015 1:00 AM"
        testReport.EndDate = "1/2/2015 6:00 PM"
        testReport.NumIntervals = 41
        PopulateDataSet(testReport)

        Serialize(testReport)
        Deserialize()

    End Sub

    Private Sub PopulateDataSet(ByRef report As Project.Report)

        report.BinnedData = New DataSet()

        Dim DT1 As DataTable = New DataTable("Test Table1")
        report.BinnedData.Tables.Add(DT1)

        DT1.Columns.Add(New DataColumn("Column1", GetType(Int32)))
        DT1.Columns.Add(New DataColumn("Column2", GetType(Int32)))
        DT1.Columns.Add(New DataColumn("Column3", GetType(Int32)))

        Dim Row1 As DataRow = DT1.NewRow()
        Dim Row2 As DataRow = DT1.NewRow()
        Dim Row3 As DataRow = DT1.NewRow()

        Row1("Column1") = 32
        Row1("Column2") = 15
        Row1("Column3") = 9

        Row2("Column1") = 3
        Row2("Column2") = 27
        Row2("Column3") = 98

        Row2("Column1") = 1
        Row2("Column2") = 12
        Row2("Column3") = 65

    End Sub

    Private Sub Serialize(ByRef report As Project.Report)

        Dim stream As New FileStream("TestReport.xxx", FileMode.Create)
        Dim mySerializer As BinaryFormatter = New BinaryFormatter()

        Try
            mySerializer.Serialize(stream, report)
        Catch ex As SerializationException
            Console.WriteLine("Error saving the report. " & ex.Message)
            Throw
        Finally
            stream.Close()
        End Try

    End Sub

    Private Sub Deserialize()

        Dim readTestReport As New Project.Report()

        Dim stream As New FileStream("TestReport.xxx", FileMode.Open)
        Dim myDeserializer As BinaryFormatter = New BinaryFormatter()

        Try
            readTestReport = DirectCast(myDeserializer.Deserialize(stream), Project.Report)
        Catch ex As SerializationException
            Console.WriteLine("Error opening the report. " & ex.Message)
            Throw
        Finally
            stream.Close()
        End Try

        Console.WriteLine(Environment.NewLine & "Values after Deserialization:")
        Console.WriteLine("Title = " & readTestReport.Title)
        Console.WriteLine("Start Date = " & readTestReport.StartDate)
        Console.WriteLine("End Date = " & readTestReport.EndDate)
        Console.WriteLine("Number of Intervals = " & readTestReport.NumIntervals)

        For Each Table In readTestReport.BinnedData.Tables
            For Each Row In Table
                Console.WriteLine("Table Row = " & Row.ToString)
                For Each DataColumn In Row
                    Console.WriteLine(DataColumn.ToString)
                Next
            Next
        Next

    End Sub

End Class

采纳答案by Andy

I was right...figured it out. The issue lay in how I was trying to display the values of the table after deserializing. DataTables and DataRows are not enumerable, but the way I was trying to display it was by looping through the way you only can with an enumerable type.

我是对的……想通了。问题在于我如何在反序列化后尝试显示表的值。DataTables 和 DataRows 是不可枚举的,但我试图显示它的方式是通过循环遍历您只能使用可枚举类型的方式。

I modified that block to assign the table to a View instead:

我修改了该块以将表分配给视图:

    Dim tableView As DataView = New DataView(readTestReport.BinnedData.Tables(0))
    Console.WriteLine("Table = ")
    Dim rowView As DataRowView
    Dim i As Integer

    For Each rowView In tableView
        For i = 0 To tableView.Table.Columns.Count - 1
            Console.Write(rowView(i) & vbTab)
        Next
        Console.WriteLine()
    Next

These articles helped:
https://msdn.microsoft.com/en-us/library/23a0aths%28v=vs.110%29.aspxhttps://msdn.microsoft.com/en-us/library/bb669099%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

这些文章有帮助:
https: //msdn.microsoft.com/en-us/library/23a0aths%28v=vs.110%29.aspx https://msdn.microsoft.com/en-us/library/bb669099%28v =vs.110%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1