I am currently having trouble finding a way to get IOrderedEnumerable from a SortedList.
I have a complex type, we just call it "A" for now, which can be decomposed into an enumerated type "A". I am currently creating a SortedList in a recursive decomposition function, where the int key is associated with the order in which the fragment is decomposed:
private static SortedList<int, A> RecursivelyBuildMySortedList(A myValue) { if (myValue == StopCondition()) { return new SortedList<int, A> { { 1, myValue } }; } var left = RecursivelyBuildMySortedList(myValue.Left); var right = RecursivelyBuildMySortedList(myValue.Right).Select(entry => new KeyValuePair<int, A>(entry.Key + left.Count, entry.Value)).ToList(); right.ForEach(pair => left.Add(pair.Key, pair.Value)); return left; }
However, I do not want to set SortedList for consumers, since the value of the key associated with the decomposition order has little value for consumers (especially as int ). The only thing the consumer should take care of is that the final order of the parts was such that each piece can be processed in the correct order. I would rather expose IOrderedEnumerable users. I thought it would be a fairly simple task, since the SortedList is very much like OrderedEnumerable in many ways, but so far it has not been able to find a good transform:
public static IOrderedEnumerable<A> Decompose(A myValue) { SortedList<int, A> mySortedList = RecursivelyBuildMySortedList(myValue);
Does anyone have a method to extract an IOrderedEnumerable from a SortedList for consumption? As a remark, I know that:
return mySortedList.Select(keyValuePair => keyValuePair.Value);
I would return an IEnumerable that would preserve the order under the hood, but because of the importance of the order in which the enumerated is processed, I would prefer the return type to be descriptive enough to indicate that the base collection is ordered (to make API is more readable).
LiamK source share