VB.NET - 循环数据表并比较行值 [英] VB.NET - Looping through datatables and compare row values
问题描述
我正在使用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
Next
Next
请帮帮我。
谢谢
Please help me.
Thanks
推荐答案
正如已经指出的那样,将所有行相互比较。您写道,要求是仅将行进行比较,但第一个问题是:行的索引是否真正定义要比较的行?
我是什么意思是:
- 两个表中的行数是否相同?
- 是否添加到表中的行,以便dt中的第2行真的是与dt2中的第2行相同,而不是第3行?
说完之后,你拥有相同数量的行,然后只需循环遍历一个表并获取相应的行。类似于:
As already pointed out you compare all of the rows with each other. You wrote that the requirement is to compare rows only with each other but the first question is: Is the index of the row really defining the rows to compare?
What I mean is that:
- do you have the same amount of rows in both tables?
- are the rows added to the tables so that row 2 in dt is really the same thing as row 2 in dt2 and not the row 3?
Having that said, of you have the same amount of rows then simply loop through one table and fetch corresponding row. Something like:
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 If
Next 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 If
Next dt2_row
< b>建议:了解调试器并查看附加到行的内容。
您需要类似以下内容:
Advice: Learn about debugger and look what append to row.
You need something like:
Go to first row of Dt
Go to first row of Dt2
repeat
do your stuff here
go to next row of Dt
go to next row of Dt2
until End of Dt or End of Dt2
你还需要学习如何访问您的数据
You also need to learn how to access your data
您发布的代码会将dt1中的每一行与dt2中的每一行进行比较(如果它编码正确)。你要做的是:
- 得到最大行数(dt1或dt2)。
The code you posted would compare each row from dt1 with each other row from dt2 (if it's coded right). What you have to do is :
- get the maximum number of rows (either in dt1 or dt2).
NumberOfRows = Math.Max(dt1.rows.count , dt2.rows.count)
- 使用此变量进行循环,但检查行是否已分配并实例化
- make a loop with this variable, but check if the rows are assigned and instanced
for i as integer = 0 to NumberOfRows -1
' check and compare here
next
- 比较条目,如果不相等,你的行动
- compare the entries and if not equal do your action
这篇关于VB.NET - 循环数据表并比较行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!