【发布时间】:2010-12-05 19:05:00
【问题描述】:
如何将 var 类型转换/转换为 List 类型?
这段代码 sn-p 给了我错误:
List<Student> studentCollection = Student.Get();
var selected = from s in studentCollection
select s;
List<Student> selectedCollection = (List<Student>)selected;
foreach (Student s in selectedCollection)
{
s.Show();
}
【问题讨论】:
-
var不是类型,它只是您分配给变量的任何表达式类型的占位符。在这种情况下,查询表达式的计算结果为IEnumerable<Student>。