2011-06-02 71 views
3

由於代碼的最後一個..看看字典項在字典中

var dictionary = new Dictionary<string, string>{ 
    { "something", "something-else" }, 
    { "another", "another-something-else" } 
}; 

dictionary.ForEach(item => { 
    bool isLast = // ... ? 

    // do something if this is the last item 
}); 

我基本上是想看看我用一個foreach迭代的容器內工作的項目是在字典中的最後一個項目。我試圖

bool isLast = dictionary[ item.Key ].Equals(dictionary.Last()) ? true : false; 

,但沒有奏效...

+1

您是否嘗試過使用計數器並通過foreach比較迭代到字典的大小?字典在理論上是無序的,所以我不會相信與字典的Last值的任何比較。 – 2011-06-02 14:55:49

+1

字典中的「last」究竟是什麼? – user7116 2011-06-02 16:39:36

回答

7

Dictionary.Last返回KeyValuePair,並且您只是將它與一個鍵的值進行比較。你不是需要檢查:

dictionary[item.Key].Equals(dictionary.Last().Value)

而且IAbstract是正確的,你可能需要使用OrderedDictionary。

+0

謝謝,我沒有想過這個。 – Ciel 2011-06-02 15:09:03

2

你將要使用的OrderedDictionary<TKey, TValue>。檢查MSDN ref.

使用標準字典,項目不保證以任何特定的順序持久。

1

你可以測試是否值== dictionary.Values.Last();

0

首先,沒有ForEach擴展方法爲Dictionary或甚至IEnumerable。所以你必須先解決這個問題。

其次,Last擴展方法將是痛得慢慢因爲它必須枚舉整個集合。第三,我不確定從具有不可預知的順序的集合中最後一個項目做些特別的事情會有很大的意義,但這大多與您的具體問題相切。

下面是我將如何解決這個問題。創建兩個在IEnumerable<T>實例上運行的新擴展方法。 ForEach將等於方法,並且WithIndex將返回另一枚枚舉器,其中包含順序索引和IsLast標誌。這是another one of my answers的一個變化,以類似的問題。

dictionary.WithIndex().ForEach(
    (item) => 
    { 
    var kvp = item.Value; // This extracts the KeyValuePair 
    if (item.IsLast) 
    { 
     Console.WriteLine("Key=" + kvp.Key.ToString() + "; Value=" + kvp.Value.ToString()); 
    } 
    }); 

這裏是新的擴展方法。

public static class ForEachHelperExtensions 
{ 
    public sealed class Item<T> 
    { 
     public int Index { get; set; } 
     public T Value { get; set; } 
     public bool IsLast { get; set; } 
    } 

    public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action) 
    { 
     foreach (T item in enumerable) 
     { 
      action(item); 
     } 
    } 

    public static IEnumerable<Item<T>> WithIndex<T>(this IEnumerable<T> enumerable) 
    { 
     Item<T> item = null; 
     foreach (T value in enumerable) 
     { 
      Item<T> next = new Item<T>(); 
      next.Index = 0; 
      next.Value = value; 
      next.IsLast = false; 
      if (item != null) 
      { 
       next.Index = item.Index + 1; 
       yield return item; 
      } 
      item = next; 
     } 
     if (item != null) 
     { 
      item.IsLast = true; 
      yield return item; 
     } 
    } 
} 
1

對循環以外的最後一項執行操作會不會更簡單?

string requiredForSomething = dictionary.Last().Value; 
1

您可以隨時使用計數器做到這一點。

int itemsCount = yourDictionary.Count; 
bool isLast = false; 

foreach(var item in yourDictionary) 
{ 
    itemsCount--;  
    isLast = itemsCount == 0; 

    if(isLast) 
    { 
    // this is the last item no matter the order of the dictionary   
    } 
    else 
    { 
    //not the last item 
    } 

} 
1

有人提及到當前項目的在反覆比較的最後一個項目的價值,例如:

dictionary[item.Key].Equals(dictionary.Last().Value) 

警告:這可能導致真正的任何如果該項目的值等於字典中最後一個項目的值,則該項目在字典中。這不是指示該項目是字典中的最後一個項目。


相反,如果你真的想找到,如果在迭代當前項是最後一項,我建議比較關鍵,因爲你知道它是獨一無二的,因此,它可能看起來如:

item.Key.Equals(dictionary.Last().Key)