Vb.net 数据表。使用和条件选择不';我不能给出预期的结果
我正在尝试比较两个数据表,并使用Vb.net 数据表。使用和条件选择不';我不能给出预期的结果,vb.net,select,datatable,Vb.net,Select,Datatable,我正在尝试比较两个数据表,并使用DataTable进行一些测试。在两个相同的数据表上选择: Using DT_NewData As DataTable = DT_DBData.Copy For x As Short = 0 To DT_NewData.Rows.Count - 1 Dim SelRows As DataRow() = DT_DBData.Select( _ "Type='" & DT_NewData.Rows(x)("Typ
DataTable进行一些测试。在两个相同的数据表上选择:
Using DT_NewData As DataTable = DT_DBData.Copy
For x As Short = 0 To DT_NewData.Rows.Count - 1
Dim SelRows As DataRow() = DT_DBData.Select( _
"Type='" & DT_NewData.Rows(x)("Type") & "'" & _
" AND In_Date='" & DT_NewData.Rows(x)("In_Date") & "'" & _
" AND Out_Date='" & DT_NewData.Rows(x)("Out_Date") & "'")
Next
但是SelRows.Length
始终为0。我的代码出了什么问题?尽管该表的副本有点奇怪,但请尝试以下操作:
假设Type
是String
Imports Microsoft.VisualBasic
Imports System.Linq
Module StartupModule
Sub Main()
' Declare the table.
Dim originalDataTable As New DataTable
' Declare the table columns.
With originalDataTable.Columns
.Add("Type", GetType(String))
.Add("InDate", GetType(DateTime))
.Add("OutDate", GetType(DateTime))
End With
' Delegate to add rows.
Dim addRow As Action(Of String, DateTime?, DateTime?) = Sub(text, inDate, outDate)
Dim newRow As DataRow = originalDataTable.NewRow()
With newRow
If (Not String.IsNullOrEmpty(text)) Then
.SetField(Of String)("Type", text)
End If
If (inDate.HasValue) Then
.SetField(Of DateTime?)("InDate", inDate.Value)
End If
If (outDate.HasValue) Then
.SetField(Of DateTime?)("OutDate", outDate.Value)
End If
End With
originalDataTable.Rows.Add (newRow)
End Sub
' Adding rows to the table.
addRow("type1", #2/2/2017#, Nothing)
addRow(Nothing, #1/25/2016#, Nothing)
addRow(Nothing, Nothing, #1/30/2016#)
' Copy the table
Dim copiedDataTable As DataTable = originalDataTable.Copy
' Loop through copied table rows.
For i As Integer = 0 To copiedDataTable.Rows.Count - 1
Dim type As String = copiedDataTable.Rows(i).Field(Of String)("Type")
Dim inDate As DateTime? = copiedDataTable.Rows(i).Field(Of DateTime?)("InDate")
Dim outDate As DateTime? = copiedDataTable.Rows(i).Field(Of DateTime?)("OutDate")
' Using DataTable Select.
Dim filter As String = String.Format("{0} {1} {2}",
If(type Is Nothing, "( Type Is Null )", String.Format("( Type = '{0}' )", type)),
If(Not inDate.HasValue, "And ( InDate Is Null )", String.Format("And ( InDate = '{0}' )", inDate.Value.ToString())),
If(Not outDate.HasValue, "And ( OutDate Is Null )", String.Format("And ( OutDate = '{0}' )", outDate.Value.ToString())))
Dim usingSelectRows As DataRow() = originalDataTable.Select(filter)
Console.WriteLine("usingSelectRows.Count = {0}", usingSelectRows.Count)
' Using Linq.
Dim typeSelector As Func(Of DataRow, Boolean) = Function(r)
If (IsNothing(type)) Then
Return IsNothing(r.Field(Of String)("Type"))
Else
Return r.Field(Of String)("Type") = type
End If
End Function
Dim inDateSelector As Func(Of DataRow, Boolean) = Function(r)
If (Not inDate.HasValue) Then
Return Not r.Field(Of DateTime?)("InDate").HasValue
Else
Return r.Field(Of DateTime?)("InDate").GetValueOrDefault.CompareTo(inDate.GetValueOrDefault) = 0
End If
End Function
Dim outDateSelector As Func(Of DataRow, Boolean) = Function(r)
If (Not outDate.HasValue) Then
Return Not r.Field(Of DateTime?)("OutDate").HasValue
Else
Return r.Field(Of DateTime?)("OutDate").GetValueOrDefault.CompareTo(outDate.GetValueOrDefault) = 0
End If
End Function
Dim usingLinqRows = From r In originalDataTable.AsEnumerable
Where
(typeSelector(r)) AndAlso
(inDateSelector(r)) AndAlso
(outDateSelector(r))
Select r
Console.WriteLine("usingLinqRows.Count = {0}", usingLinqRows.Count)
Console.WriteLine()
Next
Console.ReadLine()
End Sub
End Module
始终使用扩展名DataRow
来检索数据。您应该使用选项Strict On
,这样它就不会编译。您不能仅使用字符串将对象与&
合并。我假设In\u Date
和Out\u Date
是日期列。关闭选项Strict后,通过使用ToString
将其自动转换为字符串。这不会返回数据表所需的日期的固定区域性格式。请选择当前区域性的格式。所以使用DT_NewData.Rows(x).Field(Of Date)(“In_Date”).ToString(CultureInfo.InvariantCulture)
@TimSchmelter Ok。我理解我的错误。无论如何,当字段为DBNull
时,您的建议将返回无效强制转换异常。你能建议另一种方法吗?你试过了吗?是否也使用DBNull
数据字段?在我的场景SelRows中,长度为0。@genespos我已经用一个完整的例子编辑了我的答案。谢谢你的努力,但我不是专家,我正在尝试通过比较每个单元格(排序后)来解决问题。没问题,我的朋友。问题从数据表中的空值开始。希望我的例子有帮助。