对于www.example.com中的每个循环vb.net

unftdfkk  于 2022-11-19  发布在  .NET
关注(0)|答案(8)|浏览(154)

如何在www.example.com中使用for循环vb.net

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

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

8ulbf1ek

8ulbf1ek1#

首先,如果你只是想给下一行的引用赋一个不同的列表,不要创建一个New的客户列表。这样做有点愚蠢。可以这样做:

Dim customers As List(Of Customer) = dataAccess.GetCustomers()

然后,对于循环,你需要一个简单的“For”循环,而不是for each。不要忘记在列表结束前停止:

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

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

For each item As Customer in  Customers.Skip(500)
   'Do something with "item" here
Next
njthzxwz

njthzxwz2#

请尝试以下操作

For Each current In customers
    ' Do something here 
    Console.WriteLine(current.Name)
Next
zaq34kh6

zaq34kh63#

类似这样的东西:-

Dim customers as New List(Of Customer)

Customers=dataAcess.GetCustomers()

For Each customer AS Customer in  Customers
   '' // do something with the customer object
Next

编辑

听起来你想从N个项目中选择500个,或者下500个。你可以使用LINQ扩展方法.Take和/或.Skip来实现这一点。然后使用ToList创建你的列表。例如:-

Dim customers as List(Of Customer)
customers = dataAccess.GetCustomers().Skip(500).Take(500).ToList()

如果你只想通过客户进行枚举,那么你可以省去ToList()。

acruukt9

acruukt94#

“这将从500开始,一直进行到结束......”

for start as integer = 500 to Customers.Count

'process customer....
customer = Customers(start)

Next

要迭代整个列表:

for each cust as Customer in Customers

Next

需要注意的是.... VB不区分大小写,您的示例代码似乎使用了小写和大写客户

v440hwme

v440hwme5#

Dim start as Integer
Dim customers as New List(Of Customers)

Customers = dataAcess.GetCustomers()

For i as Integer = start to Customers.count Step 500
    Debug.Print Customers(i).someProperty
    Do something here
Next i

我认为您需要使用客户索引和500s中的步骤。这将只处理客户(开始)、客户(开始+500)、客户(开始+1000)等,而不是所有客户。这是您的意图吗?

du7egjpx

du7egjpx6#

我不太清楚你想干什么,但也许你可以试试这个:

Dim count As Integer = 0
Const MAX_CUSTOMERS As Integer = 500
Dim customers As List(Of Customers) = dataAcess.GetCustomers()

For Each c As Customer In customers
    'do stuff here'
    c.Name = "Billy"
    count+=1

    If count = MAX_CUSTOMERS Then Exit For
Next

这并不优雅,但它是有意义的,它会工作。

flvlnr44

flvlnr447#

显然,这里并不缺少各种各样的对象。我不认识您的“DataAcess”对象类型,但是如果您可以将该表作为Recordset(即SQL表)拉入,那么请尝试这样做。(完成后不要忘记返回记录集)

Option Explicit

Dim Customers As Recordset
Set Customers=currentdb.openrecordset("???")

While Customers.EOF=False
   'do stuff here using the recordset object
   'i.e. Customers.Fields("Name")="Billy"
   Customers.MoveNext
Wend
m528fe3b

m528fe3b8#

您可以尝试:

For i As Integer = 500 To (customers.Count -1)
        ''do something
    Next

或者

For i As Integer = 0 To (customers.Count - 1) step 500
        ''do something
    Next

相关问题