I have a dictionary which is sorted like this:
var sortedDict = (from entry in dd
orderby entry.Value descending select entry
).ToDictionary(pair => pair.Key, pair => pair.Value);
How can I select top 10 from this sorted dictionary?
As you mention descending in your query, I assume that you need Last 10 Occurences. If so
var sortedDict = (from entry in dd orderby entry.Value descending select entry)
.Take(10)
.ToDictionary(pair => pair.Key, pair => pair.Value) ;
var sortedDict = dd.OrderByDescending(entry=>entry.Value)
.Take(10)
.ToDictionary(pair=>pair.Key,pair=>pair.Value);
If you need first 10, just remove descending
and it will work fine.
var sortedDict = (from entry in dd orderby entry.Value select entry)
.Take(10)
.ToDictionary(pair => pair.Key, pair => pair.Value) ;
var sortedDict = dd.OrderBy(entry=>entry.Value)
.Take(10)
.ToDictionary(pair=>pair.Key,pair=>pair.Value);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With