Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through Generic List in vb.net

Tags:

vb.net

In my VB.net application I am populating my customer object and looping through it as shown below.

As there are thousands of customers, I want to do it 500 customers at a time.

Is there anyway I can have one more For loop to process 500 customers at one shot in vB.net

I am not using LinQ as the database is 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
like image 316
acadia Avatar asked Jan 23 '23 06:01

acadia


2 Answers

You can loop through blocks of 500 Customer objects like this:

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.

EDIT:

If you mean that you only want to process the first 500 Customer objects, try this:

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.

Also, you can use VB's IsNot keyword in the If statement, like this:

If CustomerList IsNot Nothing Then
like image 53
SLaks Avatar answered Jan 30 '23 03:01

SLaks


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

http://msdn.microsoft.com/en-us/library/5ebk1751.aspx

like image 20
KV Prajapati Avatar answered Jan 30 '23 04:01

KV Prajapati