6.2. Query a Nongeneric Collection

Problem

You need to query data that is stored in a collection that implements IEnumerable, such as an ArrayList, rather than IEnumerable(Of T).

Solution

Create a standard LINQ query, such as the one described in recipe 6-1, but strongly type the iterator variable used in the From clause.

How It Works

LINQ queries support collections that implement IEnumerable(Of T) by default. Nongeneric collections, such as an ArrayList, are not supported by default because the extension methods that make up the standard query clauses do not extend IEnumerable. A typical query, assuming names implements IEnumerable(Of T), looks something like this:

Dim query = From name In names

If names were an ArrayList, the query would ...

Get Visual Basic 2008 Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.