LINQ: why does this query not work on an ArrayList?

Since ArrayList allows you to collect objects of different types, the compiler doesn't know what type it needs to operate on.

The second query explicitly casts each object in the ArrayList to type Student.

Consider using List<> instead of ArrayList.


In the second case, you're telling LINQ what the type of the collection is. ArrayList is weakly typed, so in order to use it effectively in LINQ you can use Cast<T>:

IEnumerable<Student> _set = lstStudents.Cast<Student>();

The array list is untyped so you have to define what type you expect. Use the List class which is strongly typed with generics.

List<Student> lstStudents = GetStudentAsArrayList();
var res = from  r in lstStudents select r;

Tags:

C#

Linq