2017-01-18 27 views
0

逆順でOrderedDictionaryを反復処理してそのキーにアクセスするにはどうすればよいですか?それは私が次のことを試してみましたLINQの拡張をサポートしていませんので:OrderedDictionaryで逆順に反復する方法

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

そしてKey

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

あなたが逆のか、通常の順序でそれを繰り返す場合ドキュメンテーションはSortedDictionary <処理鍵、TValue>クラスの要素とは異なり、OrderedDictionaryの要素がキーでソートされていない*、と述べているので、それは問題ではありません。 。* –

+0

挿入順が私がOrderDictionaryを使用している理由です。それに応じて逆順に反復処理する必要があります。 –

+0

辞書のkeysプロパティを使用して、常に反復処理を行うことができます。 – jdweng

答えて

2

あなたは(あなたの要件に応じて、またはSortedDictionary)定期Dictionaryを使用することにより大幅にこの問題の複雑さを軽減し、キーを追跡するために、二次Listを保つことができます挿入順序。あなたも、この組織を容易にするためのクラスを使用することができます。

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]]; 
    } 
} 

(そしてもちろんのは、あなたが同様に必要とする他のどんな方法で追加してください。)

+0

これは私が目指していることを達成し、さらなるカスタマイズのための制限が少なくなります。 –

1

あなたはこのようにインデックスにある要素を取得することができます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()) 
{ 
} 

イワンStoevはコメントで指摘したように、OrderedDictionaryの返された項目は、すべてで注文されていないので、SortedDictionaryはあなたが望むものです。

+0

私は試してみるつもりだ、私には合理的な音。 –

+2

ElementAtのO(n)複雑さに注意してください。コレクションの各要素にこれを適用すると複雑さが増します(間違った場所で使用するとかなり厄介になります) – spender

+0

@spenderはその事実を認識していませんでした。それを持っていただきありがとうございます。 –

2

月を取得する:

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

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

私は誤っていない場合は、挿入順序をそのままにしておく必要があります.SortedDictionaryは挿入順序を保持しません。 –

+0

次に、「Dictionary 」を使用してください。 –

+0

辞書では、ここで説明したように、挿入オーダーが保持されているとは思われません:http://stackoverflow.com/questions/16694182/ordereddictionary-and-dictionary –

0

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); 
      } 
1

私は事実に悩まされていません。キーをインデックス可能なコレクションにコピーすることによって、キーを取得できます。また、ループの状態を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--) 
    { 
     // gives me the value, how to get the key ? e.g. "something" and "another" 
     var value = orderedDictionary[dictIndex]; 
     //get your key e.g. "something" and "another" 
     var key = keys[dictIndex]; 
    } 
0

を、それはLINQの拡張をサポートしていないので... 。

これは非汎用のEnumerableです。適切なタイプにキャストすることで汎用にすることができます。

foreach (var entry in orderedDictionary.Cast<DictionaryEntry>().Reverse()) { 
    var key = entry.Key; 
    var value = entry.Value; 
} 
関連する問題