Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - var to List<T> conversion

Tags:

var

list

c#-3.0

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();
}
like image 344
user366312 Avatar asked Oct 11 '09 17:10

user366312


1 Answers

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();
like image 160
Christian C. Salvadó Avatar answered Sep 21 '22 11:09

Christian C. Salvadó