Robel Haile Robel Haile - 10 months ago 46
ASP.NET (C#) Question

compare dictionary objects and get the one with lowest value

so i have this variable:

var productsInCart = (List<Dictionary<string, object>>)Session["ProductsInCart"];

The dictionarys inside the list have 3 keys. "unit_price", "name" and "quantity" with different values in them.

How can i in the best way get the dictionary objekt with the lowest unit_price and turn that unit_price into an int variable. I have no ide how to do this i have tried but have come absolutly nowhere can somebody help me?

Answer Source

Using LINQ should be quite easy. I'm assuming you need to parse unit_prices to int, be careful: This will throw an exception if the data cannot be converted to an int.

int result =  productsInCart.Select(x => int.Parse(x["unit_price"])).Min();