Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

9
  • 2
    The last one should be .Values and not a select clause. Commented Sep 23, 2016 at 10:05
  • 2
    @Mafii Are you sure? Values returned by OrderBy are not of a KeyValuePair type, they have no Value field. Exact type I see here is IOrderedEnumerable<KeyValuePair<TKey, TValue>>. Perhaps you meant something else? Can you write a complete line showing what you mean (and test it)? Commented Sep 23, 2016 at 14:30
  • 2
    I think this answer contains what I mean: stackoverflow.com/a/141105/5962841 but correct me if I confused something Commented Sep 23, 2016 at 14:34
  • 3
    @Mafii Re-read my whole answer, explanations between code sections tell the context. The answer you mention is like second code section in my answer (no order required). There I just wrote items.Value like you suggested. In the case of the fourth section that you commented, the Select() is a way to cause foreach to enumerate directly on the values in the dictionary instead of key-value pairs. If somehow you don't like the Select() in this case, you might prefer the third code section. The point of the fourth section is to show that one can pre-process the collection with LINQ. Commented Sep 23, 2016 at 15:14
  • 3
    If you do .Keys.Orderby() you'll iterate on a list of keys. If that's all you need, fine. If you need values, then in the loop you'd have to query the dictionary on each key to get the value. In many scenarios it won't make a practical difference. In high-performance scenario, it will. Like I wrote in the beginning of the answer: "there are many ways (...) and there's no best way. It depends on the need and often on taste, too." Commented Sep 23, 2016 at 15:23