What type of collection to use

I am going to be doing 2 for loops.  One cycles through one set of ProductIDs and another cycles through a different set of ProductIDs.

I want to add Each Product ID, ProductID to a collection.  I'm not sure what I should use, a hashtable

example:

foreach(Product p in myvariable1)
{
    add p.ProductID to collection and call it ParentProductID
}
foreach(Product p in myvariable2)
{
    add p.ProductID to collection and call it RelatedProductID
}

I will retrieve the hash table later and use it in a call to my method which insert each Pair into a Relationship table and so I'm gonna want to cycle through that collection and do something like this:

foreach (value pair in my collection of ProductIDs)
{
     AddRelationship (collection.ParentProductID, collection.RelatedProductID,....)
}

how should I go about collecting and passing in my situation, what do you recommend  I've never had to do something like this before believe it or not.


Answer this question

What type of collection to use

  • Mitesh Shah923

    Hi Yaar,

    Don't u find Hash Table collection is appropriate for ur requirement

    If not what is the collection suggested by u could please post that

    Thanx in advance.

    Cheers,

    Ch.T.Gopi Kumar.



  • managar

    Hi,

    Hope Hash Table Collection willl be more suitable for ur requirement.

    For more info , please see at : http://msdn2.microsoft.com/en-us/library/system.collections.hashtable.aspx

    Thanx,

    Ch.T.Gopi Kumar.



  • Renderflash

    If you felt keeping the whole Product structure in hashtable is too heavy, you can do a smaller hashtable that only contains pareint id as key, and current id as value (or the other way around)

    foreach(Product p in myvariable)
    {

    if (product has parent)

    add p.ProductID, parent id

    }

    //retrieve

    foreach (Product in my collection of Products)
    {

    if hashtable.contains(product id)

    insert (product id, hashtable[product id]);

    }




  • enric vives

    cool, thanks guys. Also good idea friendly dog.

  • ELDHOSE_BABY_a06ce9

    A hashtable will work, and I suggest you extend your product class to have a ParentProductID. The pseudo code of adding products will be

    foreach(Product p in myvariable)
    {
        add p.ProductID, p   //add to hash table with productid as key

    }
    foreach(Product p in myvariable)
    {

       p.ParentID = the id of the parent
        add p.ProductID,p //add to hash table with productid as key
    }

    Retrieve:

    foreach (Product in my collection of Products)
    {

            if (p.ParentID != -1)

                       insert (p.ParentID, p.ProductID)
         }



  • Meghan

    now that I looked at my original post, I posted it wrong. I just updated it. I was referring to 2 different arrays with 2 different sets ofproducts as you can see now with variable1 and variable2 which are arrays actually.

  • What type of collection to use