如何循环数据集并比较列值 [英] how to loop through dataset and compare column values

查看:102
本文介绍了如何循环数据集并比较列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想循环遍历数据集的行并比较特定列的值。

i am使用以下代码,但它不会循环遍历数据集中的所有行



 对于 i  As  整数 =  0    ds。表( 学生)。Rows.Count  -   1  
Dim student_fullname As String
Dim student_fullname2 As String

student_fullname = ds.Tables( 0 )。行(i)( 1 )。的ToString ()
MsgBox( student_name: + student_fullname)

对于 j 作为 整数 = 0 ds.Tables( 学生)。Rows.Count - 1
student_fullname2 = ds.Tables( 0 )。行(j + 1 )( 1 )。ToString ()
如果 字符串 .Equals(student_fullname,student_fullname2)然后
MsgBox( defaulter: + student_fullname)
结束 如果
下一步
下一步

解决方案

这应该在服务器端完成,而不是在客户端代码中。

这是我用来做的存储过程(通常在SSMS中,但你应该可以从代码中调用它)。



报告重复项的SQL Server程序 [ ^ ]

Quote:

我想识别那些名字在数据库表中出现多次的学生



尝试以下Linq解决方案

  Dim 重复作为列表(  字符串)= ds .Tables(  Students)。AsEnumerable()。[选择](功能(dr)dr.Field(   String )(  StudentName))。GroupBy(函数(x)x)。其中(函数(g)g.Count()>  1 )。[选择](功能 (g)g.Key)。ToList()


嗯,不 - 它不会。

你正在使用学生表选择要迭代的行数,但第一个(第0个)表要比较值。因此,如果学生表不是第一个,您将检查错误的行数。如果是,那么你几乎可以立即找到学生。



你在第一次检查时超过了可能的行数:你使用0来( count - 1)作为循环值,然后在循环中的行号中加1 - 所以你检查值1来计数。并且由于只有计数值,你超过可用的行。

假设有三行,并且计数是3.所以j将是0,1,2所以你将跳过第0行,然后访问第一行,第二行,并尝试访问不存在的第三行。



我认为你需要坐下来,查看你的数据并解决问题正是你想要做的事情:因为我不认为是这样,无论你想做什么!


i want to loop through the rows of a dataset and compare the values of a particular column .
i am using the following code but it does not loop through all the rows in the dataset

For i As Integer = 0 To ds.Tables("Students").Rows.Count - 1
            Dim student_fullname As String
            Dim student_fullname2 As String

            student_fullname = ds.Tables(0).Rows(i)(1).ToString()
            MsgBox("student_name: " + student_fullname)

            For j As Integer = 0 To ds.Tables("Students").Rows.Count - 1
                student_fullname2 = ds.Tables(0).Rows(j + 1)(1).ToString()
                If String.Equals(student_fullname, student_fullname2) Then
                    MsgBox("defaulter: " + student_fullname)
                End If
            Next
 Next

解决方案

That should be done on the server side, not in client code.
This is a stored procedure I use to do that (usually in SSMS, but you should be able to call it from code).

SQL Server Procedure to Report Duplicates[^]


Quote:

i want to identify those students whose names are present more than once in the database table


try below Linq solution

Dim duplicates As List(Of String) = ds.Tables("Students").AsEnumerable().[Select](Function(dr) dr.Field(Of String)("StudentName")).GroupBy(Function(x) x).Where(Function(g) g.Count() > 1).[Select](Function(g) g.Key).ToList()


Well, no - it won't.
You are using the "Students" table to select the number of rows to iterate through, but the first (zeroth) table to compare values against. So if the "Students" table isn't the first one, you will check the wrong number of rows. And if it is, then you will always find the student almost immediately.

And you are exceeding the number of possible rows on the first check: you use 0 to (count - 1) as the loop values, and then add 1 to the row number in the loop - so you check values 1 to count. And since there are only count values you exceed the rows available.
Assume there are three rows, and count is 3. So j will be 0, 1, 2 So you will skip the zeroth row, then access row one, row two, and try to access row three which doesn't exist.

I think you need to sit down, look at your data and work out exactly what you are trying to do: because I don't think that is it, whatever it is you are trying to do!


这篇关于如何循环数据集并比较列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆