0

I have created a dictionary (differentKeys). For getting top 2 elements I am using below code.

topKeys[B] = (from entry in differentKeys orderby entry.Value descending 
             select entry)
                .ToDictionary(pair => pair.Key, pair => pair.Value).Take(2)
                .ToDictionary(x=>x.Key,x=>x.Value).Values.ToArray();

But it seems not giving expected outcome. Can you please suggest line in C# which will return me top 2 maximum elements?

enter image description hereenter image description here

Pratik K. Shah
  • 397
  • 3
  • 17

3 Answers3

3

It's not clear why you keep converting to dictionaries all the time. The order of entries Dictionary<,> is not guaranteed.

It looks like you just want:

var topValues = differentKeys.Values
                             .OrderByDescending(x => x)
                             .Take(2)
                             .ToArray();

Or if you want the keys which correspond to the top values:

var keysForTopValues = differentKeys.OrderByDescending(x => x.Value)
                                    .Select(x => x.Key)
                                    .Take(2)
                                    .ToArray();
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • The same issue still . See I edited my question putting snaps – Pratik K. Shah Apr 09 '14 at 10:00
  • @PratikK.Shah: What do you mean by "same issue"? This will give you the top two values in the dictionary. Did you actually want "the keys corresponding to the top two values"? If so, that's very different - it's important to be clear in what you're asking for. – Jon Skeet Apr 09 '14 at 10:08
  • Actually if it is, {46,5} {36, 1} {41,7} {30, 9} {20,4} {26, 6} Then I need 30, 41 – Pratik K. Shah Apr 09 '14 at 10:16
  • @PratikK.Shah: So as I said, you want the keys corresponding to the top values. My edited answer should give you that. *Please* put more effort into writing a clear question to start with next time... and posting *text* of input and expected output is far, far better than posting screenshots. – Jon Skeet Apr 09 '14 at 10:20
1

Not sure what your expected output and the actual output is, but you seem to be wanting to get top 2 from your dictionary.

Dictionary<string, string> sample = new Dictionary<string, string>();
sample.Add("First", "Yasser");
sample.Add("Second", "Amit");
sample.Add("Third", "Sachin");
sample.Add("Fourth", "Kunal");

Dictionary<string, string> top2 = sample.Take(2).ToDictionary(m => m.Key, m => m.Value);

Update : Just noticed your were using "descending" in your code.

Incase you want to sort on key use this

sample.OrderByDescending(m => m.Key).Take(2)
Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
0

You should not keep converting to dictionaries all the time, also, what type is differentKeys really? I'm assuming here that it is some kind of IEnumerable<T>. If differentKeys is a IDictionary<K,V>, go with Jon's answers, ie use the Values property for the collection of values instead of selecting them via Linq.

topKeys[B] = (from entry in differentKeys orderby entry.Value descending 
              select entry.Value)
              .Take(2)
              .ToArray();
flindeberg
  • 4,887
  • 1
  • 24
  • 37