I'm trying to query a DataTable to get the list of ID's (integers) that correspond to a specific criteria, such that I can then parse that list into a joined string that can be used as a filter on a DataView :
Dim dvFilteredList As New DataView(dtbList)
Dim existingSelections = (From dtr In dtbMain.AsEnumerable
Where dtr.Field(Of String)("Field1") = strField1Value _
And dtr.Field(Of String)("Field2") = strField2Value _
And dtr.Field(Of String)("Field3") = strField3Value _
And Not IsDBNull(dtr.Field(Of Integer)("SomeID"))
Select New With {.SomeID = dtr.Field(Of Integer)("SomeID")}).ToList
Dim strViewFilter As String = "[SomeID] NOT IN (" & String.Join(",", existingSelections) & ")"
dvFilteredList.RowFilter = strViewFilter
When I try to query the list of integer ID's into existingSelections
, I get this exception :
Cannot cast DBNull. Value to type 'System.Int32'. Please use a nullable type.
How can I query the qualifying ID's into a list (or array) that I can then parse?
CodePudding user response:
The Field
extension method supports nullable types, so use it correctly:
AndAlso dtr.Field(Of Integer?)("SomeID").HasValue
(also use AndAlso
instead of And
everywhere for performance reasons)