0
我完全不知道爲什麼linq會拋出一個「System.Int32」鍵入'System.String'異常。該代碼是通用函數的一部分,用於將DataTextField和DataValueField從無類型的DataTable中提取出來,並將它們放在KeyValuePair(string,string)的列表中。我爲linq查詢上的每個循環添加了一個遍歷每個項目的代碼,並且工作正常。爲DataValueField返回的數據是數字,但是它是在一個無類型的集合中。我在字段上嘗試了ToString(),不確定接下來要嘗試什麼。任何幫助,將不勝感激。Linq to Untyped Datatable中的對象無法將類型爲'System.Int32'的對象轉換爲鍵入'System.String'
Private Function GetCurrentBoundControlDT(ByVal StoredProcedure As String, ByVal ParamList As String, ByVal DataTextField As String, ByVal DataValueField As String) As List(Of KeyValuePair(Of String, String))
Dim ds As DataSet = Nothing
Dim dt As DataTable = Nothing
Dim BoundControlDataList As List(Of KeyValuePair(Of String, String)) = Nothing
If ParamList.Trim <> String.Empty Then
StoredProcedure = String.Format("{0} {1}", StoredProcedure, ParamList)
End If
ds = RG.GetDS(StoredProcedure)
If Not ds Is Nothing AndAlso ds.Tables.Count > 0 Then
dt = ds.Tables(0)
'This works fine
For Each r As DataRow In dt.Rows
Dim test1 As String = r(DataTextField)
Dim test2 As String = r(DataValueField)
Dim kv As New KeyValuePair(Of String, String)(test1, test2)
Next
'Blows up here...
BoundControlDataList = (From ThisTable In dt.AsEnumerable() _
Select New KeyValuePair(Of String, String)(ThisTable.Field(Of String)(DataTextField).ToString(), _
ThisTable.Field(Of String)(DataValueField).ToString())).ToList()
End If
Return BoundControlDataList
End Function