无法将类型 'System.Data.DataTable' 的对象转换为类型 'System.Collections.IEnumerable'

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

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

添加填充数据集的方法后,反序列化函数给出错误:无法将类型 'System.Data.DataTable' 的对象转换为类型 'System.Collections.IEnumerable'。 它不应该被序列化为数据集...而不是数据表吗?我不确定到底是什么导致了我这个错误。代码片段如下。感谢您的帮助!

<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

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

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

    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

这些文章帮助了:
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