How to cast/convert a var type to a List type?
This code snippet is giving me error:
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();
}
When you do the Linq to Objects query, it will return you the type IEnumerable<Student>
, you can use the ToList()
method to create a List<T>
from an IEnumerable<T>
:
var selected = from s in studentCollection
select s;
List<Student> selectedCollection = selected.ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With