在vb.net中循环遍历通用列表

aca*_*dia 4 vb.net

在我的VB.net应用程序中,我填充了我的客户对象并循环遍历它,如下所示.

由于有成千上万的客户,我想一次做500个客户.

无论如何我还可以再用一个For循环来在vB.net中一次性处理500个客户

我没有使用LinQ,因为数据库是Oracle.

有没有像

谢谢

Dim customerList as new List(of customer)
Dim Customer as new Customer

Try
CustomerList=dataAccess.GetAllCustomers()

if not CustomerList is nothing then

   For each Customer in CustomerList
       BuildXML(custmer.Name,Customer.age,Customer.city)
   next
       ProcessXMLWS(strxml)
end if

Catch ex as exception
   LogError(ex.message)
End try
Run Code Online (Sandbox Code Playgroud)

SLa*_*aks 6

您可以循环遍历500个Customer对象的块,如下所示:

For i As Integer = 0 To CustomerList.Count Step 500
    'Do things
Next
Run Code Online (Sandbox Code Playgroud)

但是,它对你没有任何好处.您的代码Customer单独使用每个对象,因此您一次只能执行500个操作.

编辑:

如果您的意思是只想处理前500个Customer对象,请尝试以下方法:

For i As Integer = 0 To Math.Min(500, CustomerList.Count)
    Set Customer = CustomerList(i)

    BuildXML(custmer.Name, Customer.age, Customer.city)
Next
Run Code Online (Sandbox Code Playgroud)

顺便说一句,你不应该写Dim Customer As New Customer- 通过添加New关键字,你创建一个Customer你从不使用的额外实例.相反,写入Dim Customer As Customer以声明变量而不创建新Customer实例.

此外,您可以IsNot在If语句中使用VB的关键字,如下所示:

If CustomerList IsNot Nothing Then
Run Code Online (Sandbox Code Playgroud)