Monthly Archives: July 2008

Cast an ArrayList as IEnumerable (and Even IQueryable)

Again, not really sure why this works. Based on the original from here.

ArrayList students = GetStudents();
var query =
  from student in students.Cast()
  where student.Score > 80
  select new { student.ID, student.Name };

This is what I ended up with:

IQueryable dcs = 
(IQueryable)getChildControlsByType(ContactDetailsView, "System.Web.DynamicData.DynamicControl").Cast().AsQueryable();

Where the method getChildControlsByType is returning a generic ArrayList (but always of some kind of control). I guess the “Cast,” being an extension method, returns a typed IEnumerable, which can be turned into an IQueryable. Without the Cast, this code throws an exception.