2013-01-03 29 views
0

在windows phone中,我在LINQ中以字典的形式檢索數據庫中的表。我的字典裏是這樣如何將字典值轉換爲列表?

Dictionary<int, StudentTable> 

我檢索表從LINQ作爲

var studentDictionary = studContext.StudentList.Select(p => new { p.RollNo, p }).AsEnumerable().ToDictionary(kvp => kvp.RollNo, kvp => kvp); 

但我想在studentDictionary所有值作爲

List<StudentTable> 

我知道這是可能通過使用for循環將每個字典值添加到列表中。 我怎樣才能做到這一點沒有循環。 ? 有沒有比循環更好的方法?

由於

+0

其他任何方法都將在內部做一個循環,無論如何,所以你爲什麼不寫循環,有關於發生的事情列表中並沒有什麼更多的自由? – Bazzz

回答

10

您可以使用studentDictionary.Values

PS:對於按鍵同樣的作品,使用studentDictionary.Keys

+1

...如果你真的想要一個列表:'studentDictionary.Values.ToList()' – afrischke

+0

除了'Dictionary .ValueCollection'是一個ICollection ',而不是'IList '。很容易解決,但;調用'.ToList()'就可以了。 –

0

你的字典包含ValueStudentTable類型的對象。您可以從字典中選擇並應用ToList方法。

var list = studentDictionary.Values.ToList(); 

var list = studentDictionary.Select(r => r.Value).ToList(); 
+0

@Downvoter,關心評論? – Habib