This feels like an obvious requirement, but LINQ is fighting me.
I have collection of objects that I need to order. So my interface returns an IOrderedEnumerable
.
Requirements change, and now I only need to take the first 10 elements. So I use .Take()
, but now it returns IEnumerable
(technically it is returning an OrderedPartition
, but that doesn't implement IOrderedEnumerable
so I can't even cast). To conform to my existing interface, I now need to do the nasty (order, then take, then order again):
Dictionary<string, int> tagCounts = ...
IOrderedEnumerable<KeyValuePair<string, int>> orderedTags = tagCounts
.OrderByDescending(kvp => kvp.Value)
.Take(10)
.OrderByDescending(kvp => kvp.Value);
Surely this is a problem that has been solved. Is there a nice way to take the top x ordered items, and keep them ordered?