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 循环将每个字典值添加到列表中。我怎么能在没有循环的情况下做到这一点。? 除了循环还有其他更好的方法吗?

谢谢

4

2 回答 2

10

您可以使用studentDictionary.Values.

PS:同样适用于键,使用studentDictionary.Keys

于 2013-01-03T08:56:00.290 回答
0

您的字典包含Valuewhich 是 type 的对象StudentTable。您可以从字典中选择它并应用ToList方法。

var list = studentDictionary.Values.ToList();

或者

var list = studentDictionary.Select(r => r.Value).ToList();
于 2013-01-03T08:55:23.863 回答