2

So I have A dictionary (Employees2Name) Of int => (some class) which I need to turn into a sorted list of key value pairs of int => (some property in the class)

I have this working fine which is the good news. It just seems like I'm doing an extra step is there a way to shorten this in linq with a cast.

ComboBoxValues.Employees2Name.Select(k => new {Key = k.Key, Value = k.Value.Name})
                             .ToDictionary(k => k.Key, v => v.Value)
                             .ToList<KeyValuePair<int, string>>()
                             .OrderBy(kp => kp.Value)

The second to dictionary seems redundant.

  • The call to `ToList` that you have accomplishes nothing productive. – Servy Jan 30 '15 at 15:30
  • I agree, I was only using it to get the keyValuepair in a list form, I forgot you could use a select to cast to the KVP –  Jan 30 '15 at 16:49
  • `Dictionary` *already* implements `IEnumerable>`. You need *none* of those operators. You don't need `Cast`, you don't need `Select`, and you don't need `ToList`. You need *nothing*. – Servy Jan 30 '15 at 16:52
  • I need it sorted by value. –  Jan 30 '15 at 16:55
  • 1
    You call `OrderBy` to do that, not `ToList`, `Cast`, or `Select`. – Servy Jan 30 '15 at 16:56
  • I didn't think you could use order by on a dictionary, when I did that I got a runtime error `At least one object must implement IComparable.` –  Jan 30 '15 at 16:59
  • 1
    Then the key you were projecting out via `OrderBy` wasn't comparable. That has nothing to do with any of the rest of this. – Servy Jan 30 '15 at 17:02
  • Thanks, your way is working and seems the most efficient –  Jan 30 '15 at 17:04

4 Answers4

2

It seems that all you need is

ComboBoxValues.Employees2Name
  .Select(k => new KeyValuePair<int, string>(k.Key, k.Value.Name))
  .OrderBy(item => item.Value);

Just Select and OrderBy; try no to materialize (i.e. ToList(), ToDictionary()) especially in the middle of the Linq.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

No need to use select and orderby. You can just try this

SortedList<int, string> sortedList = 
    new SortedList<int, string>(ComboBoxValues.Employees2Name
                                 .ToDictionary(i => i.Key, i => i.Value.Name));
serdar
  • 1,564
  • 1
  • 20
  • 30
0

Dictionary class already implements IEnumerable>, that is a valid input for your OrderBy() then applying a ToList>() seems totally useless.

More, I think that the ToDictionary call is a waste of memory and time, because you are constructing the dictionary (which main purpose is to keep items unique by key) from a plain collection of items and later sort them by value (rather than key), thus without taking any advantage from the Dictionary<,> class.

I would rewrite your code as

ComboBoxValues.Employees2Name.Select(k => new KeyValuePair<int, string>(k.Key, k.Value.Name))
                             .OrderBy(kp => kp.Value)

Regards, Daniele.

Rubidium 37
  • 661
  • 6
  • 12
0

@Servy Comments reflects the best answer.

You already have this in an
IEnumerable<KeyPairValue<int, Class>> you just need to put the name to a dictionary then order by

@Html.PopulateCombobox(ComboBoxValues.Employees2Name
                                     .ToDictionary(k => k, v => v.Value.Name)
                                     .OrderBy(v => v.Value)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
johnny 5
  • 19,893
  • 50
  • 121
  • 195