Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在vb.net中为一个对象中的多个列表循环它_Vb.net_Arraylist_Dataset - Fatal编程技术网

如何在vb.net中为一个对象中的多个列表循环它

如何在vb.net中为一个对象中的多个列表循环它,vb.net,arraylist,dataset,Vb.net,Arraylist,Dataset,我有一个数据集,它存储从数据库中获取的数据 我的数据集如下所示: Id EmpId Questions 1 6 abcdefgh 2 6 kjkjlkjj 3 6 yiuyuiyi 4 6 kljkljkl 我必须遍历数据集并将值分配给我的类,如下所示 Public class MyClass Public Property Id As Intege

我有一个数据集,它存储从数据库中获取的数据

我的数据集如下所示:

Id     EmpId     Questions
 1        6       abcdefgh
 2        6       kjkjlkjj
 3        6       yiuyuiyi
 4        6       kljkljkl
我必须遍历数据集并将值分配给我的类,如下所示

   Public class MyClass
      Public Property Id As Integer = 0
      Public Property EmpId As Integer = 0
      Public Property Question As List(of String)
   End Class
如何循环数据集,创建一个对象,并将多个问题作为列表(分配给MyClass对象)提取到“问题”属性中


有什么帮助吗?提前感谢。

这里有一个简单的例子:

Public Function ReturnQuestions() As List(Of MyClass)
    Dim questions As New List(Of MyClass)()
    Using ds As DataSet = GetData("select Id, EmpId, Questions from table")
        //check if populated
        If (ds IsNot Nothing) AndAlso (ds.Tables.Count > 0) AndAlso (ds.Tables(0).Rows.Count > 0)
            For Each row As DataRow In ds.Tables(0).Rows
                questions.Add(New MyClass(ds(0), ds(1), ds(2)))
            Next
        End If
    End using
    Return questions
End Function
对于dataset.tables.rows上的每个循环(首先检查是否为nothing)

然后在循环内部执行一个myList.Add(新的MyClass(ds(0),ds(1),…)


最后,您将有一个从DS解析的列表。有多种方法可以设计for循环的内部部分如果您有任何问题,请联系首先,您不能将任何内容存储在
数据集中,而不是
数据表中,在
数据表中有数据。因此,为了简化问题,它应该是

如何在数据集中循环遍历数据表

答案很简单

首先,抓取数据表

Dim Dt as DataTable = dataset.Table("table name")
For Each Row in Dt
 Dim value1 as String = Row(0) ' Here 0 is the column index,change it as required
Next
现在遍历数据表

Dim Dt as DataTable = dataset.Table("table name")
For Each Row in Dt
 Dim value1 as String = Row(0) ' Here 0 is the column index,change it as required
Next
现在回到问题的第二部分:

将值分配给我的类

首先,您需要创建类
MyClass
列表

Dim myClassList as New Lsit(Of MyClass)
现在,在循环中,您可以执行以下操作:

For Each Row in Dt
 Dim Class1 as New MyClass
 class1.Id= Convert.ToInt32(Row(0)) ' Here 0 is the column index,change it as required
 '''assign other values as well(if required)
 myClassList.Add(Class1)
Next

希望这有帮助:)

我的列表是什么?你能用一个伪代码解释一下吗?答案没有解释,没有格式,但是,Uselessorry all在手机上-正确答案w/example在MyClass的构造函数内,你可以设置每个值。如果您不想使用构造函数,请使用With语法:您的意思是希望每个EmpID都有一个MyClass,并为该EmpID列出一系列问题吗?顺便说一句,数据集包含数据表。这是您显示的数据表。谢谢@zack。这真的很有帮助:)@Santosh,非常欢迎你。。我希望将来也能帮助你:)