Manipulate a Sorted collection and update results

peter liles 556 Reputation points
2023-06-24T14:04:20.05+00:00

I have the following SortedList collection. The key is a unique ProductID value whereas the value represents a Product item.

I am trying to find all product objects that have multiple Vendors (i.e with matching VendorID(UniqueIdentifier) properties). Then discount ShipingFee(decimal) property by 10% from each Vendors Product object. With the exception of those with a Free ShippingFee that equal 0.0000 (database value).

That way i can reduce the delivery charge to customers who purchase multiple items from same Vendor. (Makes sense)

I imagine some Linq expressions method would be suitable

Dim ShoppingCart As Collections.Generic.SortedList(Of Integer, Product) = (System.Web.HttpContext.Current.Session("cart"))

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,598 questions
{count} votes

4 answers

Sort by: Most helpful
  1. QiYou-MSFT 4,326 Reputation points Microsoft External Staff
    2023-07-03T06:36:35.19+00:00

    Hi @peter liles

    According to my understanding, as long as the data in this table exists with the same VendorID, its ShippingFee will be reduced by 10%. Then we can divide the data into several groups according to the VendorID, and then count the number of data in the group, and then proceed to the next step.

    Code:

     Dim query=from list in products
      group list by list.VendorID;
    
     For Each listgroup In query
            For Each list In listgroup
                Dim number As Integer = listgroup.Count()
                list.ShippingFee = list.ShippingFee * Math.Pow(0.9, number - 1)
                Console.WriteLine($"	{listgroup.Count()},{list.VendorID},{list.ProductID},{list.Description},{list.Price},{list.ShippingFee}")
            Next
        Next
    
    
    

    Output:

    Picture1

    If there is a discrepancy with your request, please update the request further. I will continue to help you solve the problem.

    Best Regards

    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. peter liles 556 Reputation points
    2023-06-25T15:33:46.94+00:00

    The Product object is stored in session state. It is not a question of ProductID (key) as my requirement is to filter Product object values on VendorID property and then to update the shipping value. In addition it has become apparent i also need to Likewise find the lowest ItemPrice property value in the associated vendorIDs and to decrease that by a percentage amount too.

    So it is not a simple problem!

    Thanks


  3. peter liles 556 Reputation points
    2023-06-28T11:39:15.0466667+00:00
    Public Class Product
     Private ProductID As Integer
     Private Description As String
      Private Price As Decimal
        Private PercentDeduction As Double  
        Private ShippingFee As Decimal
        Private Delivery_Service_Provider As String
        Private VendorName As String
        Private VendorID As Guid
    End Class
    
    
    
    var ShoppingCart = New SortedList(Of Integer, Product) {
        new Product {VendorID="3c8a0060-977d-4774-861c-4c43084389d5", ProductID=123, Description = "Computer", Price = 700, ShippingFee =25 },
        new Product {VendorID="67e6bc48-0ba9-46c0-9330-e5f080457de9", ProductID=1234, Description = "Bike", Price = 200, ShippingFee = 20 },
        new Product {VendorID="3c8a0060-977d-4774-861c-4c43084389d5", ProductID=1835, Description = "Car", Price = 3500, ShippingFee 0.0000 },
        new Product {VendorID="3c8a0060-977d-4774-861c-4c43084389d5", ProductID=2378, Description = "Car", Price = 2700, ShippingFee = 70 },
       new Product {VendorID="17a6b024-7a82-4fce-8566-67259a99d6c4", ProductID=1678, Description = "Watch", Price = 1000, ShippingFee = 10 },
       new Product {VendorID="17a6b024-7a82-4fce-8566-67259a99d6c4", ProductID=3348, Description = "Cooker", Price =900, ShippingFee = 50 }
    };
    Note: SortedList has ProductID as the Key
    
    I want to reduce (nominal %) the Price of the Product with the lowest Price when a Vendor purchases additional items.
    Also, reduce (nominal %) the ShippingFee of each additional Product.  
    
    Ignore ShippingFee = 0.0000.
    
    I probably want to be able to give the Vendor the option to decide either to reduce the ShippingFee else Price or both?
    
    
    

    I hope this is more helpful.

    It is difficult to realise when you are using just your imagination !


  4. peter liles 556 Reputation points
    2023-07-04T20:04:19.3766667+00:00

    Thanks a lot my good friend!

    I had given up because of many things. For a start my shoppingcart changes all the time because of update and deletion.Also the value would increase on every page postback so i figured i could not update the sortedlist and bind to control. Then i thought about showing changes in another separate list and only save changes when at checkout to view latest changes on receipt statement - Bingo!

    The other ideas are to do with the filters these appear difficult to implement in practice as they interfere with other properties that also need to be set as my ShoppingBasket and Receipts are calculated different; so, if possible add a product filter to ignore the highest price item and discount all other vendor items on TotalPrice (Sub Total field) instead of both price and shippingfee!

    Also, the collection should not include Vendors with a single item in collection. Just multiple product items per Vendor.

    The Discount value is not a set figure it is variable dependant upon Vendor table entry.

    I must mention that all examples are written in C# language and differ from VB.

    Therefore could you please convert to VB version. I have tried without success !

    Much obliged.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.