In most of the LINQ examples you will find in the Web and the official MS documentation, LINQ queries are run on strongly-typed collections (that is collections that implement IEnumerable<T> interface).
What if you wanted to run a query expression on a non-generic collecton (an ArrayList for example).
For instance, yor code might look like this:
ArrayList customers = new ArrayList();
customers.Add(...);
....
//build the content of the customers list
...
//now query it
var query = from c in customers
where c.Name == "John"
select c.City;
If you try to compile the code above, the c# compiler will greet you with a nice error message which says:
Could not find an implementation of the query pattern for source type 'System.Collections.ArrayList'. 'Where' not found. Consider explicitly specifying the type of the range variable
You can do one of the following to make the code compile and your query running:
a) call the Cast<T>() method.
Cast<T>() is an extension method defined in the System.Linq assembly. It extends IEnumerable and converts it to IEnumerable<T>.
So with the call to the Cast<T>() method our sample query would look like this:
var query = from c in customers.Cast<Customer>()
where c.Name == "John"
select c.City;
...and now everything nicely compiles and works.
b) use explicitly typed range variable. The c# compiler allows you to explicitly specify the type of the range variable within the query.
The compiler, I believe calls Cast<T>() under the hood.
So our sample query becomes:
var query = from Customer c in customers
where c.Name == "John"
select c.City;
This looks much prettier (at least according to me) than the previous example with the call to Cast<T>().