对于vb.net中的每个循环

9 .net vb.net foreach for-loop

我如何在vb.net中使用for循环

dim start as integer
Dim customers as New List(Of Customers)

Customers=dataAcess.GetCustomers()

For each start=500 in  Customers.count
  'Do something here'
Next
Run Code Online (Sandbox Code Playgroud)

我想为每500个客户处理一些数据..请帮助

Joe*_*orn 9

首先,如果您要在下一行为参考分配不同的列表,请不要创建"新"客户列表.那有点蠢.像这样做:

Dim customers As List(Of Customer) = dataAccess.GetCustomers()
Run Code Online (Sandbox Code Playgroud)

然后,对于循环,您需要一个简单的"For"循环而不是每个循环.不要忘记在列表结尾之前停止:

For i As Integer = 500 To Customers.Count -1 
    'do something with Customers(i) here
Next i
Run Code Online (Sandbox Code Playgroud)

如果您使用的是Visual Studio 2008,您也可以这样写:

For each item As Customer in  Customers.Skip(500)
   'Do something with "item" here
Next
Run Code Online (Sandbox Code Playgroud)


Jar*_*Par 7

请尝试以下方法

For Each current In customers
  '' // Do something here 
  Console.WriteLine(current.Name)
Next
Run Code Online (Sandbox Code Playgroud)