I have 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 carry out some calculations on product properties e.g discount ShipingFee(decimal) property and the lowest obtained ItemPrice by 10% from each Vendors Product object.
With the exception of those with a ShippingFee equal to 0.0000 (database value).
The sortedlist should then be updated with the new ShippingFee and ItemPrice values in their original respective element
Example
vendor1 ,20.39
vendor2 ,10,30
vendor2 ,10,20
output
vendor2 11,22

Deepak RawatRank #646 · 3.7K reputationPosted Jun 26, 2023, 12:59 AM EDT
To find the product objects with multiple vendors and perform calculations on their properties in the described manner, you can iterate over the
SortedListcollection and use a dictionary to keep track of products with multiple vendors. Here's an example implementation:SortedList sortedList = GetSortedList(); // Your SortedList collection // Dictionary to track products with multiple vendors Dictionary> productsWithMultipleVendors = new Dictionary>(); // Iterate over the sortedList foreach (KeyValuePair kvp in sortedList) { Product product = kvp.Value; // Check if the product has already been added to productsWithMultipleVendors if (productsWithMultipleVendors.ContainsKey(product.ProductID)) { productsWithMultipleVendors[product.ProductID].Add(product); } else { // Add the product to productsWithMultipleVendors if it has multiple vendors List productList = sortedList.Values.Where(p => p.ProductID == product.ProductID).ToList(); if (productList.Count > 1) { productsWithMultipleVendors.Add(product.ProductID, productList); } } } // Perform calculations on product properties for products with multiple vendors foreach (KeyValuePair> kvp in productsWithMultipleVendors) { Guid productID = kvp.Key; List productList = kvp.Value; decimal lowestItemPrice = decimal.MaxValue; // Find the lowest ItemPrice and calculate the new ShippingFee foreach (Product product in productList) { if (product.ShippingFee != 0.0000m) { lowestItemPrice = Math.Min(lowestItemPrice, product.ItemPrice); } } decimal newShippingFee = lowestItemPrice * 0.1m; // Update the original sortedList with the new ShippingFee and ItemPrice values foreach (Product product in productList) { if (product.ShippingFee != 0.0000m) { product.ShippingFee = newShippingFee; product.ItemPrice -= lowestItemPrice * 0.1m; } } } // The sortedList has been updated with the new valuesGetSortedList()is a placeholder for the method that retrieves or initializes yourSortedListcollection. You would need to replace it with your actual implementation to obtain the sorted list of products.Please note that the above code assumes you have a
Productclass with properties likeProductID,VendorID,ShippingFee, andItemPrice. Adjust the code according to your actualProductclass structure.peterRank #5,346 · 302 reputationPosted Jun 26, 2023, 10:22 AM EDT
I got the output wrong as results should be
vendor2 ,11,30
vendor2 ,11,22
As i want to update all multiple vendor records with new shippingfee and itemprice ( only the lowest value)
Thanks
peterRank #5,346 · 302 reputationPosted Jun 26, 2023, 10:50 AM EDT
Another issue that i did not include was the SortedList constructor. The key is a ProductID integer value whereaas in your example it is VendorID value. So i would not be able to update the list? and i need to keep this original sortedlist constructor with ProductID as key when insert display inside listview and database table.
ShoppingCart = CType(System.Web.HttpContext.Current.Session("Cart"), Collections.Generic.SortedList(Of Integer, Product))