Tutorial :Assigning max values from a dictionary



Question:

I need to assign values from a small dictionary collection (10 items) to a larger number (about 20 to 30) of dropdownlists in their selected value property.

Specifically, I need to take the key of the max value in the dictionary, assign it to the first dropdownlist.selected value. Then take the key of the 2nd highest max value, assign it to the second dropdownlist. When I go through all the keys/values in the dictionary (and there's still dropdownlists needing to be assigned), I need to start back at the top using the key of the max value in the dictionary.

I'm able to get the key of the max value like so.

var maxKey =       results      .Aggregate((left, right) => left.Value > right.Value ? left : right).Key;    ddlItem.SelectedValue = maxKey.ToString();  

What I was doing originally was, I was removing the maxvalue after assigning it. But I forgot that there would be more dropdownlists than actual dictionary values. I'm guessing the solution is to use a different, temporary collection and work with that? Any help is appreciated.

If using a list is required, how would I transfer a dictionary to a list and use index, key, and value?


Solution:1

You can use OrderByDescending to get the key value pairs in descending order by value, eg

var d = new Dictionary<int, string>();  var orderedKeyValuePairs = d.OrderByDescending(kvp => kvp.Value);  

In this example, orderedKeyValuePairs is an IOrderedEnumerable<KeyValuePair<int, string>>

In order to facilitate assigning to your dropdowns, you could create a utility class that wraps a Queue as follows:

public class KVPCycle  {     Queue<KeyValuePair<int, string>> queue;        public KVPCycle(IEnumerable<KeyValuePair<int, string>> items)     {         queue = new Queue<KeyValuePair<int, string>>(items);     }       public KeyValuePair<int, string> Next()     {      var item = queue.Dequeue();      queue.Enqueue(item);      return item;     }    }  

You can use it like this:

var cycle = new KVPCycle(d.OrderByDescending(kvp => kvp.Value));    // Loop over your dropdown lists  for each dropdown list        var kvp = cycle.Next();        // Get the key      int key = kvp.Key;        // Get the value      string val = kvp.Value;        // Now assign to your dropdown      // ...  

In this way, once you get to the end of your ordered list you'll automatically be back at the start again - the values rotate in a cycle.


Solution:2

Are you trying to create an IEnumerable of keys that are sorted (DESC) by value?

edit

The simplest way to do this would be:

var seq = d.OrderByDescending(kvp => kvp.Value).Select(kvp => kvp.Key);  

The d is your Dictionary<string,int>. The OrderByDescending sorts the key-value pairs by diminishing value and the Select extracts the key from the pair. The result is an IEnumerable<string> in exactly the order you want.

At that point, you can run a foreach over it, or I suppose you could also load it into a Queue, as another example shows.

(The above could also be done in a more SQL-ish syntax, but I didn't see the point.)


Solution:3

You should sort the list descending, then you can assign the first value to the first control, second value to the second control, and so on, by index.

Hope that helps!


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »