Results 1 to 13 of 13

Thread: [RESOLVED] converting List to Array

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    18

    Resolved [RESOLVED] converting List to Array

    Hello guys!

    I wanted to create two lists and then convert them to arrays, with the data from this table:

    idSuplier Item Price
    1 Item A 10.00
    1 Item B 20.00
    1 Item C 30.00
    2 Item A 20.00
    2 Item B 30.00
    2 Item C 40.00
    3 Item A 30.00
    3 Item B 40.00
    3 Item C 50.00

    Ok! the desired result would be: (1, 60.00); (2, 90.00) and (3, 120.00)

    So, I've tried the following code to create my array, but I haven't any results!

    Code:
    Private Sub List()
            
    
            Dim idSupplier As List(Of Integer) = New List(Of Integer)
            Dim sumSupplier As List(Of Decimal) = New List(Of Decimal)
            Dim value As Decimal
    
            For i = 1 To j
                For Each col As DataGridViewRow In dgItems.Rows
                    If col.Cells(0).Value = i Then
                        value = value + CDec(col.Cells(2).Value)
                        sumSupplier.Add(value)
                        idSupplier.Add(i)
    
    
    
                    End If
    
    
                Next
                
            Next i
            Dim idSupplierArray() = idSupplier.ToArray()
            Dim sumSupplierArray() = sumSupplier.ToArray()
    
            
            Console.ReadLine()
    
        End Sub
    Sorry if It seems so dumb, but its what I am...any hint?
    Last edited by goku99; Apr 11th, 2013 at 11:05 PM.

  2. #2
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: converting List to Array

    why the list should converted to array, what you are trying to do
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  3. #3
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: converting List to Array

    assuming you actually DO have a datagrid, and any other errors aside, it looks like your logic on your for each is jacked up. You aren't resetting value to zero after each iteration for one thing, and you are adding to the new array upon each match, not after making a total. if everything else is working, you would get an array the same size as the original but with partially summed lines. your example would end up1,10;1,30;1,60; etc.
    at the very least make this change:
    Code:
     For i = 1 To j
          value = 0            
          For Each col As DataGridViewRow In dgItems.Rows
                    If col.Cells(0).Value = i Then
                        value = value + CDec(col.Cells(2).Value)
                    end if       
          next       
                sumSupplier.Add(value)
                 idSupplier.Add(i)
                Next
                
            Next i
    as for whether or not there are other issues, i can't say since i don't have all your project. for example i have no idea why you are doing "i = 1 to j" since you haven't assigned "j" a value anywhere.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  4. #4
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: converting List to Array

    Hi,

    Here is one way you can do this using LINQ to Objects:-

    Code:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
      Dim myTableSummary = (From myRow As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)() Where Not myRow.IsNewRow Select ID = myRow.Cells(0).Value, _
                            ItemValue = myRow.Cells(2).Value Group By ID Into G = Group, TotalItems = Count(), ValueOfItems = Sum(CDec(ItemValue)))
     
      Dim myArrayOfAnonymousTypes() = myTableSummary.ToArray
      For Each myElement In myArrayOfAnonymousTypes
        MsgBox(myElement.ID.ToString & " - " & myElement.TotalItems.ToString & " - " & myElement.ValueOfItems.ToString)
      Next
    End Sub
    This will create an initial Enumeration of Anonymous Types in the MyTableSummary variable but if you specifically want to convert this to an Array then you can just call the .ToArray method of the Enumeration as demonstrated above.

    Hope that helps.

    Cheers,

    Ian

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: converting List to Array

    The best solution is to put a breakpoint on the outer for statement and step through the code watching what is happening. We can suggest alternatives, or sit and stare at what you did looking for a problem, but you have the code and you have the means to watch what it is doing line by line. If you haven't used the debugger before, ask about that, as it is the single most powerful tool at your disposal, and would find you the answer you seek in this, and many other, cases.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    18

    Re: converting List to Array

    Hi Make me Rain!
    I wish to return the sum of the Items for each idSuplier, and display which has the lowest price, highest price and the order among then. So, I was thinking in using a sorted Array, and then bring the information through labels, like: "The lowest price of the list is from "@idsuplierx@", with the amount of "@"; the highest price is from "@idspupliery@"...and the others are...
    So, I want to to use a loop to put the data into two Lists: one for the supplier IDs and one for the sums of the prices, then convert each List to an array and then use Array.Sort to sort both arrays using the sums as the keys. For that I would create a List(Of Integer) for the IDs and a List(Of Decimal) for the prices. I'd then loop through the DataTable and, for each row, I'd get the index in the ID List of the current ID. If it was found then I'd add the current price to the value at the same index in the other List. If it wasn't found then I'd add the ID to one List and the price to the other. Once the loop is done, I would call the overload of Array.Sort with the two arrays as arguments. The first parameter I'd pass the price List first. I'd use a For loop to get each ID and corresponding price sum from the two arrays, which will be sorted lowest price sum to highest.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    18

    Re: converting List to Array

    Quote Originally Posted by Lord Orwell View Post
    assuming you actually DO have a datagrid, and any other errors aside, it looks like your logic on your for each is jacked up. You aren't resetting value to zero after each iteration for one thing, and you are adding to the new array upon each match, not after making a total. if everything else is working, you would get an array the same size as the original but with partially summed lines. your example would end up1,10;1,30;1,60; etc.
    at the very least make this change:
    Code:
     For i = 1 To j
          value = 0            
          For Each col As DataGridViewRow In dgItems.Rows
                    If col.Cells(0).Value = i Then
                        value = value + CDec(col.Cells(2).Value)
                    end if       
          next       
                sumSupplier.Add(value)
                 idSupplier.Add(i)
                Next
                
            Next i
    as for whether or not there are other issues, i can't say since i don't have all your project. for example i have no idea why you are doing "i = 1 to j" since you haven't assigned "j" a value anywhere.
    Thanks Lord! But I still cannot "cast" the results...

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    18

    Re: converting List to Array

    Quote Originally Posted by IanRyder View Post
    Hi,

    Here is one way you can do this using LINQ to Objects:-

    Code:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
      Dim myTableSummary = (From myRow As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)() Where Not myRow.IsNewRow Select ID = myRow.Cells(0).Value, _
                            ItemValue = myRow.Cells(2).Value Group By ID Into G = Group, TotalItems = Count(), ValueOfItems = Sum(CDec(ItemValue)))
     
      Dim myArrayOfAnonymousTypes() = myTableSummary.ToArray
      For Each myElement In myArrayOfAnonymousTypes
        MsgBox(myElement.ID.ToString & " - " & myElement.TotalItems.ToString & " - " & myElement.ValueOfItems.ToString)
      Next
    End Sub
    This will create an initial Enumeration of Anonymous Types in the MyTableSummary variable but if you specifically want to convert this to an Array then you can just call the .ToArray method of the Enumeration as demonstrated above.

    Hope that helps.

    Cheers,

    Ian
    Thanks Ian! It seems to work! But I still have to figure out how to display the lowest sum, the highest and the others among them.

    Thanks Ian! It really worked!

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    18

    Re: converting List to Array

    Quote Originally Posted by Lord Orwell View Post
    assuming you actually DO have a datagrid, and any other errors aside, it looks like your logic on your for each is jacked up. You aren't resetting value to zero after each iteration for one thing, and you are adding to the new array upon each match, not after making a total. if everything else is working, you would get an array the same size as the original but with partially summed lines. your example would end up1,10;1,30;1,60; etc.
    at the very least make this change:
    Code:
     For i = 1 To j
          value = 0            
          For Each col As DataGridViewRow In dgItems.Rows
                    If col.Cells(0).Value = i Then
                        value = value + CDec(col.Cells(2).Value)
                    end if       
          next       
                sumSupplier.Add(value)
                 idSupplier.Add(i)
                Next
                
            Next i
    as for whether or not there are other issues, i can't say since i don't have all your project. for example i have no idea why you are doing "i = 1 to j" since you haven't assigned "j" a value anywhere.
    Lord, j represents the number of suppliers...

  10. #10
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: converting List to Array

    Hi,

    You can use MIN and MAX functions in the LINQ query to get the Minimum and Maximum value for each group. i.e:-

    Code:
    MaxValue = Max(CDec(ItemValue)), MinValue = Min(CDec(ItemValue))
    As to other calculations you need doing, these all need to be Aggregate functions due to the fact that you are working within Groups of Items.

    Hope that helps.

    Cheers,

    Ian

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    18

    Re: converting List to Array

    Quote Originally Posted by IanRyder View Post
    Hi,

    You can use MIN and MAX functions in the LINQ query to get the Minimum and Maximum value for each group. i.e:-

    Code:
    MaxValue = Max(CDec(ItemValue)), MinValue = Min(CDec(ItemValue))
    As to other calculations you need doing, these all need to be Aggregate functions due to the fact that you are working within Groups of Items.

    Hope that helps.

    Cheers,

    Ian
    Hi, thanks again! Sorry being a dumb, but although you have given me tips, when I implemented your code, I got the error message:
    "InvalidCastExceptions was unhandled
    Conversion from string " & myElement.MaxValue.ToString &" to type 'Double' is not valid."

    Here is the code:
    Code:
    Dim myTableSummary = (From myRow As DataGridViewRow In dgItensPesquisado.Rows.Cast(Of DataGridViewRow)() Where Not myRow.IsNewRow Select ID = myRow.Cells(0).Value, _
                            ItemValue = myRow.Cells(2).Value Group By ID Into G = Group, TotalItems = Count(), ValueOfItems = Sum(CDec(ItemValue)), MaxValue = Max(CDec(ItemValue)), MinValue = Min(CDec(ItemValue)))
    
            Dim myArrayOfAnonymousTypes() = myTableSummary.ToArray
            For Each myElement In myArrayOfAnonymousTypes
                MsgBox(myElement.ID.ToString & " - " & myElement.TotalItems.ToString & " - " & myElement.ValueOfItems.ToString - " & myElement.MaxValue.ToString & ")
    
                
            Next

  12. #12
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: converting List to Array

    Hi,

    You have got the structure of the message incorrect. You are actually trying to do math calculations with strings as you have it now. It should say this:-

    Code:
    MsgBox(myElement.ID.ToString & " - " & myElement.TotalItems.ToString & " - " & myElement.ValueOfItems.ToString & " - " & myElement.MaxValue.ToString)
    Hope that helps.

    Cheers,

    Ian

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    18

    Re: converting List to Array

    Hey, Thanks again! Topic closed! Thanks a lot!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width