How to Load a Generic class without Loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nomaneagle
    New Member
    • Nov 2008
    • 8

    How to Load a Generic class without Loop

    ok this is the thing I have right now which is working quite well beside its a bit slow:

    Public Function GetList() As List(Of SalesOrder)
    Try
    Dim list As New List(Of SalesOrder)

    Dim ds As DataSet

    ds = cls.GetSalesOrd erList 'CLS is the data access class


    For i = 0 To ds.Tables(0).Ro ws.Count - 1

    Dim row As DataRow = ds.Tables(0).Ro ws(i)
    Dim kk As SalesOrder = New SalesOrder()


    kk.ID = Val(row.Item("i d") & "")
    kk.SalesOrderNo = row.Item("sales orderid") & ""
    kk.SalesOrderDa te = row.Item("Order Date") & ""
    kk.CustomerId = Val(row.Item("c ustomerid") & "")

    list.Add(kk)

    Next
    Return list

    Catch ex As Exception
    Throw ex
    End Try

    End Function

    Now once I start retrieving more than 10000 records from the table, the loop takes long time to load values into generic class. Is there any way that I can get rid of loop? Can I do something like the following with the generic class?

    txtSearch.AutoC ompleteCustomSo urce.AddRange(A rray.ConvertAll (Of DataRow, String)(Busines sLogic.ToDataTa ble.ConvertTo(W orkOrderList).S elect(), Function(row As DataRow) row("TradeConta ctName")))
  • Aimee Bailey
    Recognized Expert New Member
    • Apr 2010
    • 197

    #2
    I do something similar in work, though i stay away from data adapters as i find them ill efficient. Can i ask why you need to draw so many sales orders from a database? seems a bit unusual.

    Comment

    • Joseph Martell
      Recognized Expert New Member
      • Jan 2010
      • 198

      #3
      You can't really get rid of the loop. Even if you use some sort of AddRange variant, there is still an internal loop.

      If you are looking to squeeze every ounce of performance from this that you can I would start looking at things like how you are accessing your object and any operations you can eliminate. For example, you are referencing your columns by name when processing your row object. This requires some string matching to occur. You could use a numeric column index instead. Also, from this documentation, use a With block to refer to your kk object (after the new statement) and you may find a very slight performance increase. I'm sure ther are other things you could do.

      Finally, the best thing you can do is re-evaluate how you are running your query. It is rarely the required case to return everything from a table in a database. Filtering records out will be far more efficient if the SQL Server handles it instead of doing it locally in VB.

      If the entire table must be retrieved, then you may want to consider parrallel processing of some kind. Of course, this has its own pitfalls and difficulties.

      Comment

      Working...