如何调试“无法将类型为System.Double的对象转换为类型为System.String的对象”

时间:2019-10-23 07:14:31

标签: vb.net

我不知道问题出在哪里,但是我认为是因为baseamount列和withholding tax列是Integer类型。

Dim duplicate = From row In dt.AsEnumerable()
                Let tcode = row.Field(Of String)("TaxCode")
                Group row By tcode Into duptcode = Group
                Where duptcode.Count() = 1
                Select duptcode
For Each duptcoderows In duplicate
    For Each row In duptcoderows
        outFile.WriteLine(row.Field(Of String)("Tin") & "," & row.Field(Of String)("TaxCode") & "," & row.Field(Of String)("PayeeName") & "," & row.Field(Of String)("BaseAmount") & "," & row.Field(Of String)("WithHoldingTax"))
    Next
Next

1 个答案:

答案 0 :(得分:0)

此异常是由您在row.Field(Of Type)中进行的显式转换引起的-在您的情况下为row.Field(Of String)

此查询LINQ的结果是一个或某些数据列的类型不同-在您的情况下为Double

如果您知道列名witch是在此列上进行正确的转换,否则将以Object格式获取所有数据,如下所示:

 Console.WriteLine(row.Item("Tin") & "," &
                   row.Item("TaxCode") & "," &
                   row.Item("PayeeName") & "," &
                   row.Item("BaseAmount") & "," &
                   row.Item("WithHoldingTax"))