2010-03-16 54 views
1

假設我有一個字典,像這樣:C#從字典獲取鍵<字符串,流>

Dictionary<string, Stream> 

我怎樣才能得到一個列表,從本詞典的JUST鑰匙(或IEnumerable的或其他) ?這可能嗎?

可能枚舉字典,並逐一提取的密鑰,但我希望避免這種情況。

在我的例子中,Dictionary包含一個文件名列表(file1.doc,filex.bmp等等)以及來自應用程序另一部分的文件的流內容。

回答

3
+0

我最初試過這個,但它沒有提供我想要的東西 - 這是可以接受的嗎? var x =(IEnumerable )attachments.Keys; - 將它作爲IEnumerable字符串轉換? – Alex 2010-03-16 17:04:38

+0

KeyCollection已經實現IEnumerable :http://msdn.microsoft.com/en-us/library/3fcwy8h6.aspx,不需要轉換。 – Juliet 2010-03-16 17:16:20

+0

@alex'KeyCollection'已經是'IEnumerable ' - 不需要演員。 – 2010-03-16 17:17:16

1

Dictionary<T,T>.Keys返回KeyCollection。這具有IEnumerable接口。

所以......

foreach(string key in Dictionary<string,Stream>.Keys) 
{ 


} 
-1
public IEnumerable<long> ReturnSomeKeys() 
{ 
    var foo = new Dictionary<long, string>(); 
    return foo.Keys; 
} 

這你想要做什麼。

相關問題