If you can determine what the order should be, then I would do it like this (I selected the order by key):
Dictionary<string, string> inputs = new Dictionary<string, string>(3) { { "A", "First" }, { "Z", "Third" }, { "J", "Second" } }; var outputs = inputs.OrderBy(i=>i.Key).Select(i=>i.Value).ToArray();
This gives you an array with the indexes you requested (for example, output[0] ).
If you really want the dictionary entries coming back, you can get ienumerable from them like this (you cannot just return the dictionary because they are unordered):
var outputs = inputs.OrderBy(i=>i.Key).Select( (entry, index) => new KeyValuePair<int, string>(index, entry.Value));
Throw .ToArray() there if you need to.
If you really want the dictionary returned, try the following:
var outputs = inputs.OrderBy(i=>i.Key) .Select((entry, i) => new { entry.Value, i }) .ToDictionary(pair=>pair.i, pair=>pair.Value).Dump();
Just keep in mind that the dictionaries are not initially ordered, so if you list it, you must add a .OrderBy again.
source share