对于 vb.net 中的每个循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1328948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:12:12  来源:igfitidea点击:

For each loop in vb.net

.netvb.netforeachfor-loop

提问by

How do I use for loop in vb.net something like

我如何在 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

I want to process some data for each 500 customers.. Please help

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

采纳答案by Joel Coehoorn

First of all, don't create a Newlist of customers if you're just going to assign a different list to the reference on the next line. That's kinda dumb. Do it like this:

首先,New如果您只是要为下一行的引用分配不同的列表,请不要创建客户列表。这有点傻 像这样做:

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

Then, for the loop you need a plain "For" loop rather than a for each. Don't forget to stop before the end of the list:

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

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

If you're using Visual Studio 2008 you could also write it like this:

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

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

回答by JaredPar

Try the following

尝试以下

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

回答by Kevin LaBranche

'This will start at 500 and process to the end....

'这将从 500 开始并处理到最后......

for start as integer = 500 to Customers.Count

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

Next

To iterate the entire list:

要迭代整个列表:

for each cust as Customer in Customers

Next 

One note.... VB is case insensitive and your sample code seems to use lower case and upper case customers

一个注意事项.... VB 不区分大小写,您的示例代码似乎使用小写和大写客户

回答by AnthonyWJones

Something like this:-

像这样的东西:-

Dim customers as New List(Of Customer)

Customers=dataAcess.GetCustomers()

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

Edit

编辑

Sounds like you want to select 500 of N items or perhaps the next 500. You could use the LINQ extension methods .Takeand/or .Skipto achieve this. Use ToList then to create your list. E.g.:-

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

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

If all you want to do enum through the customers then you could dispense with ToList().

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

回答by Fredou

You can either try:

您可以尝试:

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

Or

或者

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

回答by Martin

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

I think you need to use the Customer index and Step in 500s. This will only process customer(start), Customer(start+500), Customer(start+1000) etc, not all the customers. Is that what you intend?

我认为您需要使用 Customer 索引和 Step in 500s。这只会处理客户(开始)、客户(开始+500)、客户(开始+1000)等,而不是所有客户。这就是你的意图吗?

回答by Jason

I'm not exactly sure what you're trying to do, but maybe you could try this:

我不太确定你要做什么,但也许你可以试试这个:

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

It's not elegant by any means, but it makes sense and it will work.

无论如何它都不优雅,但它是有道理的,并且会起作用。

回答by PowerUser

Obviously, there is no shortage of variety. I don't recognize your "DataAcess" object type, but if you can pull that table in as a Recordset (i.e. a SQL table) then try this. (Don't forget to return the recordset when your done)

显然,不乏多样性。我不认识您的“DataAcess”对象类型,但如果您可以将该表作为记录集(即 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