本文介绍了VB.NET - 循环数据表并比较行值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在使用visual studio(vb.net)并且有两个数据表(dt和dt2)。我想仅将每行数据表彼此进行比较 - > dt中的row1,dt2中的row1,dt中的row2,dt2中的row2,dt中的row3和dt2中的row3,依此类推。 我的代码现在看起来像这样程序将dt中的每一行与dt2中的每一行进行比较,这是不行的。I'm using visual studio (vb.net) and have two datatables (dt and dt2). I want to compare each row of datatables only with each other --> row1 in dt with row1 in dt2, row2 in dt with row2 in dt2, row3 in dt with row3 in dt2 and so on.My code now looks like this and the program compares each row in dt with each row in dt2, which is not ok.For Each row As DataRow In dt.Rows For Each row As DataRow In dt2.Rows Dim PTP_zakl As Date = row(1).ToString Dim AX_prevz As Date = row(1).ToString If PTP_zakl < AX_prevz Then sendMail Else Do nothing End If NextNext 请帮帮我。 谢谢Please help me.Thanks推荐答案For counter As Integer = 1 To dt.Rows.Count Dim PTP_zakl As Date = dt.Rows(counter)(1).ToString Dim AX_prevz As Date = dt2.Rows(counter)(1).ToString If PTP_zakl < AX_prevz Then sendMail() End IfNext counter 作为旁注,如果第1列包含一个日期,我将使用 CType 将对象转换为日期,而不是 ToString 和隐式转换。 附加: 如果可以根据列进行比较来查找匹配行可以将循环简化为如下所示:As a side note, if the column 1 contains a date I'd use CType to convert the object to the date, not ToString and implicit conversion.ADDITION:If the comparison can be done based on a column to find a matching row the loop could be simplified to something like the following:For Each dt2_row As System.Data.DataRow In dt2.Rows If (dt.Select("Manjkajoči_MA <'" & dt2_row("Prevzeti_MA").ToString & "'").Count = 0) Then sendMail() End IfNext dt2_rowGo to first row of DtGo to first row of Dt2repeat do your stuff here go to next row of Dt go to next row of Dt2until End of Dt or End of Dt2 你还需要学习如何访问您的数据You also need to learn how to access your dataNumberOfRows = Math.Max(dt1.rows.count , dt2.rows.count) - 使用此变量进行循环,但检查行是否已分配并实例化- make a loop with this variable, but check if the rows are assigned and instancedfor i as integer = 0 to NumberOfRows -1 ' check and compare herenext - 比较条目,如果不相等,你的行动- compare the entries and if not equal do your action 这篇关于VB.NET - 循环数据表并比较行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-11 18:15