如何通过 OrderedDictionary 反向迭代

How to iterate in reverse through an OrderedDictionary

如何反向遍历 OrderedDictionary 并访问它的键?

由于它不支持 LINQ 扩展,我尝试了以下方法:

var orderedDictionary= new OrderedDictionary();
orderedDictionary.Add("something", someObject);
orderedDictionary.Add("another", anotherObject);

for (var dictIndex = orderedDictionary.Count - 1; dictIndex != 0; dictIndex--)
{
    // It gives me the value, but how do I get the key?
    // E.g., "something" and "another".
    var key = orderedDictionary[dictIndex];
}

您可以像这样在索引处获取元素:

orderedDictionary.Cast<DictionaryEntry>().ElementAt(dictIndex);

并获得 Key:

orderedDictionary.Cast<DictionaryEntry>().ElementAt(dictIndex).K‌​ey.ToString();

我可以建议使用 SortedDictionary<K, V> 吗?它确实支持 LINQ 并且类型安全:

var orderedDictionary = new SortedDictionary<string, string>();
orderedDictionary.Add("something", "a");
orderedDictionary.Add("another", "b");

foreach (KeyValuePair<string, string> kvp in orderedDictionary.Reverse())
{
}

此外,正如 Ivan Stoev 在评论中指出的那样,OrderedDictionary 的返回项根本没有排序,因此 SortedDictionary 就是您想要的。

如果您需要使用 OrderdDictionary,您始终可以使用如下所示的 SortedDictionary。

var orderedDictionary = new SortedDictionary<int, string>();

orderedDictionary.Add(1, "Abacas");
orderedDictionary.Add(2, "Lion");
orderedDictionary.Add(3, "Zebera");

var reverseList = orderedDictionary.ToList().OrderByDescending(pair => pair.Value);

foreach (var item in reverseList)
{
    Debug.Print(item.Value);
}

我不介意订单事实。您可以通过将键复制到可索引集合来获取键。还需要将循环条件更改为 dictIndex > -1;.

请试试这个:

var orderedDictionary = new OrderedDictionary();
orderedDictionary.Add("something", someObject);
orderedDictionary.Add("another", anotherObject);

object[] keys = new object[orderedDictionary.Keys.Count];
orderedDictionary.Keys.CopyTo(keys, 0);

for (var dictIndex = orderedDictionary.Count-1; dictIndex > -1; dictIndex--)
{
    // It gives me the value, but how do I get the key?
    // E.g., "something" and "another".
    var key = orderedDictionary[dictIndex];

    // Get your key, e.g. "something" and "another"
    var key = keys[dictIndex];
}

您可以通过使用常规 Dictionary(或 SortedDictionary,具体取决于您的要求)显着降低此问题的复杂性,并保留辅助 List 来跟踪键的插入顺序。您甚至可以使用 class 来促进此组织:

public class DictionaryList<TKey, TValue>
{
    private Dictionary<TKey, TValue> _dict;
    private List<TKey> _list;

    public TValue this[TKey key]
    {
        get { return _dict[key]; }
        set { _dict[key] = value; }
    }

    public DictionaryList()
    {
        _dict = new Dictionary<TKey, TValue>();
        _list = new List<TKey>();
    }

    public void Add(TKey key, TValue value)
    {
        _dict.Add(key, value);
        _list.Add(key);
    }

    public IEnumerable<TValue> GetValuesReverse()
    {
        for (int i = _list.Count - 1; i >= 0; i--)
            yield return _dict[_list[i]];
    }
}

(当然还可以添加您需要的任何其他方法。)

Since it doesn't have any support for LINQ extensions...

那是因为它是 non-generic Enumerable。您可以通过将其转换为正确的类型来使其通用:

foreach (var entry in orderedDictionary.Cast<DictionaryEntry>().Reverse()) {
    var key = entry.Key;
    var value = entry.Value;
}