开发者

Looping with List(of objects) in vb.net

开发者 https://www.devze.com 2022-12-08 10:16 出处:网络
I have a procedure in VB.net using VS.net 2008 which will get the list of Orders and build a XML file with the list of Orders as shown in the code below:

I have a procedure in VB.net using VS.net 2008 which will get the list of Orders and build a XML file with the list of Orders as shown in the code below:

As the number of Orders is getting huge, I want to bu开发者_如何学JAVAild the XML file for every 500 Orders

Dim Orders as List(of Orders)=DAL.GetAllOrders()
Dim Order as new Orders

Public xmlstring As New StringBuilder

If Not Orders Is Nothing Then

xmlstring.Append("<?xml version=""1.0"" encoding=""Windows-1252"" standalone=""yes""?>")
                    xmlstring.Append("<Orders>")

For Each Order In Orders

    'Build the XML File

next

     'access web service and pass the XML file

end if

Instead of building XML for all the records I want to create XML for each 500 records. I tried the following code and it is throwing an error saying Expression is of type Orders which is not a collection type.

Please help

Dim start As Integer
  For i As Integer = start To Orders.Count Step 500
    xmlstring.Append("<?xml version=""1.0"" encoding=""Windows-1252"" standalone=""yes""?>")
                    xmlstring.Append("<Orders>")

    For Each Order In Orders(i)

    'Build the XML File

    next                 

  next


Some issues to resolve:

  • Step 500 will SKIP 500 rows at a time. I.e., it will write Order #1, then order #501, then #1001, etc. You'll need to track your orders-per-count yourself.
  • Your use of variable names for Order/Orders vs. the class names is confusing, and is tripping you up.
  • XML should be built via the XML tools in .NET, not by string concat. You're going to build yourself into a brick wall quickly.

Loop construct:

 Dim Orders as List(of Order) = DAL.GetAllOrders()
 If Not Orders Is Nothing Then
   Dim i As Integer = 1
   For Each order As Order In Orders
     If i=500 Then
        ' write XML footers, close the current file
        ' open the next file, write XML header
        i = 1
     Else
        i += 1
     End If
     ' write XML for this order
   Next
   ' Write final XML footer and close current file
 End If


it is throwing an error saying Expression is of type Orders which is not a collection type.

The variable "Orders" is a collection type (List(of Orders)), but Orders(i) is not, it is an "Orders" (whatever that is, you have not posted the definition) object.

So, you need to either implement IEnumerable in your Orders class or change this line:

For Each Order In Orders(i)


I would use something like this:

for i as integer = 0 to orders.count - 1
  <create xml here and put into variable (append new XML as needed)>
  if (i mod 500 = 0 andalso not i = 0) orelse i = orders.count - 1 then
    <output XML here and clear XML variable>
  end if
next i


How about something like this:

Dim OrdersList as List(of Orders)=DAL.GetAllOrders()


For i as int32 = 0 to OrdersList.Count() - 1
  Dim o as Orders = OrdersList(i)
  'Build your xml
  If i mod 500 = 0 Then
    'write out xml & reset your string builder
  End If

Next

*Note I changed your Orders variable as the naming convention was a little confusing and might be causing name collisions.

0

精彩评论

暂无评论...
验证码 换一张
取 消