循环遍历 vb.net 中的通用列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1331883/
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
Loop through Generic List in vb.net
提问by acadia
In my VB.net application I am populating my customer object and looping through it as shown below.
在我的 VB.net 应用程序中,我正在填充我的客户对象并循环遍历它,如下所示。
As there are thousands of customers, I want to do it 500 customers at a time.
由于有成千上万的客户,我想一次做 500 个客户。
Is there anyway I can have one more For loop to process 500 customers at one shot in vB.net
无论如何,我是否可以再使用一个 For 循环来在 vB.net 中一次性处理 500 个客户
I am not using LinQ as the database is Oracle.
我没有使用 LinQ,因为数据库是 Oracle。
is there anything like
有什么像
Thanks
谢谢
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
回答by SLaks
You can loop through blocks of 500 Customer
objects like this:
您可以Customer
像这样循环遍历 500 个对象的块:
For i As Integer = 0 To CustomerList.Count Step 500
'Do things
Next
However, it won't do you any good.
Your code is using each Customer
object individually, so there is nothing you can do with 500 at a time.
然而,这对你没有任何好处。您的代码Customer
单独使用每个对象,因此一次使用 500 个对象无能为力。
EDIT:
编辑:
If you mean that you only want to process the first 500 Customer
objects, try this:
如果您的意思是只想处理前 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
As an aside, you shouldn't write Dim Customer As New Customer
- by adding the New
keyword, you create an extra Customer
instance that you never use. Instead, write Dim Customer As Customer
to declare the variable without creating a new Customer
instance.
顺便说一句,您不应该编写Dim Customer As New Customer
- 通过添加New
关键字,您会创建一个Customer
您从未使用过的额外实例。相反,写入Dim Customer As Customer
声明变量而不创建新Customer
实例。
Also, you can use VB's IsNot
keyword in the If statement, like this:
此外,您可以IsNot
在 If 语句中使用 VB 的关键字,如下所示:
If CustomerList IsNot Nothing Then
回答by adatapost
Use counter variable.
使用计数器变量。
Dim counter as Integer = 1
For each Customer in CustomerList
If counter = 500 Then
ProcessXMLWS(strxml)
strxml="" '//Empty if any.'
counter = 1
Next
BuildXML(custmer.Name, Customer.age, Customer.city)
counter += 1
next