[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 910
  • Last Modified:

Find the smallest value in a list

I have a dictionary like below where I keep GUID and decimal value. I want to loop thru this and find the smallest value in the dictioanry. I got it to here. How can i do this?

Dictionary<Guid, decimal> shipValues = new Dictionary<Guid, decimal>();
 
decimal minValue = 0.00M;
 
        foreach (KeyValuePair<Guid, decimal> kvp in shipValues) 
        {
           //here, how do I get the least decimal value?
        
        }

Open in new window

0
Camillia
Asked:
Camillia
  • 6
  • 3
  • 3
1 Solution
 
GiftsonDJohnCommented:
Dictionary<Guid, decimal> shipValues = new Dictionary<Guid, decimal>();
 
decimal minValue = 0.00M;
bool first = true;

        foreach (KeyValuePair<Guid, decimal> kvp in shipValues)
        {
            if(first)
            {
                   minValue = kvp.Value;
                   first=false;
            }
           //here, how do I get the least decimal value?
           if(kvp.Value<minValue)
                 minValue=kvp.Value;
        }

    return minValue; //it has min value
0
 
CamilliaAuthor Commented:
Doesnt work. I only have one item in the dictionary. it's a guid and value 2.95.

2.95 is greater than minValue but this line fails :if(kvp.Value<minValue)

now, i can change it to if(kvp.Value > minValue) but not sure if that's going to solve it IF i have more values in the dictionary...
0
 
GiftsonDJohnCommented:
What is the ouput you are getting?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
CamilliaAuthor Commented:
if I have one row in Dictionaray, it just by passes the checks. i get 0.00 as value because it doesnt even compare. That comparision fails because 2.95 < 0.00 fails.
0
 
käµfm³d 👽Commented:
If you are using VS 2008 (or .NET 3.0+), then you can use the Min extension function:
Dictionary<Guid, decimal> shipValues = new Dictionary<Guid, decimal>();
 
shipValues.Add(Guid.NewGuid(), 2343M);
shipValues.Add(Guid.NewGuid(), 23M);
shipValues.Add(Guid.NewGuid(), 100M);
decimal minValue = shipValues.Values.Min();

Open in new window

0
 
GiftsonDJohnCommented:
for that I am assigning the first value using

            if(first)
            {
                   minValue = kvp.Value;
                   first=false;
            }

For the first time it assigns the first value to the minValue. After that all the values in the dictionary values are compared with minValue
0
 
CamilliaAuthor Commented:
kaufmed: - that kinda works but i dont think i can use it like that. I'm doing this in an ItemDataBound routine. So it keeps overriding the min value. I cant compare them:
 Dictionary<Guid, decimal> shipValues = new Dictionary<Guid, decimal>();
 
        decimal minValue = 0.00M;
 
        if (e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
            string id = item["Id"].Text;
            decimal shipPrice = decimal.Parse(item["EstimatedRate"].Text.Substring(1));
 
            shipValues.Add(new Guid(id), shipPrice);
            minValue = shipValues.Values.Min();
        }

Open in new window

0
 
CamilliaAuthor Commented:
i know what the problem is. I'm doing this in "ItemDataBound' So, the value in the Dictionary keeps overwiriting to the next item data bound value. So, how can i do this?
 protected void test_onItemDataBound(object sender, GridItemEventArgs e)
    {
       // List<string> shippingPrice = new List<string>();
        Dictionary<Guid, decimal> shipValues = new Dictionary<Guid, decimal>();
 
        decimal minValue = 0.00M;
 
        if (e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
            string id = item["Id"].Text;
            decimal shipPrice = decimal.Parse(item["EstimatedRate"].Text.Substring(1));
            //item["Estimatedrate"].BackColor = System.Drawing.Color.Red; //#E0EDFF"
 
            shipValues.Add(new Guid(id), shipPrice);
            minValue = shipValues.Values.Min();
        }
 
 
 
 
 
        Guid shipId = Guid.Empty;
        //decimal minValue = 0.00M;
        bool first = true;
 
        foreach (KeyValuePair<Guid, decimal> kvp in shipValues)
        {
            if (first)
            {
                minValue = kvp.Value;
                shipId = kvp.Key;
                first = false;
            }
            //here, how do I get the least decimal value?
            if (kvp.Value >= minValue)
            {
                minValue = kvp.Value;
                shipId = kvp.Key;
            }
        }
 
       // return minValue; //it has min value
 
        
       
        
    }

Open in new window

0
 
CamilliaAuthor Commented:
getting close, i moved this outside the itemdatabound routine:

 Dictionary<Guid, decimal> shipValues = new Dictionary<Guid, decimal>();
 
still have another problem. I will post back soon
0
 
käµfm³d 👽Commented:
I'm assuming this is what you were referring to in your last post, but in case it isn't...

You delcared you dictionary inside your event handler. So every time the event fires, a dictionary is created and then destroyed when the handler exits. You need to declare a class-level dictionary and add values inside the handler.

You might want to put some sentinel value in your decimal variable in case the list is empty:
Dictionary<Guid, decimal> shipValues = new Dictionary<Guid, decimal>();
 
protected void test_onItemDataBound(object sender, GridItemEventArgs e)
{
    // List<string> shippingPrice = new List<string>();
 
    decimal minValue = -1M;
 
    if (e.Item is GridDataItem)
    {
        GridDataItem item = e.Item as GridDataItem;
        string id = item["Id"].Text;
        decimal shipPrice = decimal.Parse(item["EstimatedRate"].Text.Substring(1));
        //item["Estimatedrate"].BackColor = System.Drawing.Color.Red; //#E0EDFF"
 
        shipValues.Add(new Guid(id), shipPrice);        
    }
 
    if (shipValue.Values.Count > 0)
    {
        minValue = shipValues.Values.Min();
    }
    
    return minValue; //it has min value
}

Open in new window

0
 
käµfm³d 👽Commented:
In the previous example, -1 meant that the list was empty, so there was no minimum value.
0
 
CamilliaAuthor Commented:
i think it works now. Let me test some more and will post back
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now