How to: Query an ArrayList with LINQ

When using LINQ to query non-generic IEnumerable collections such as ArrayList, you must explicitly declare the type of the range variable to reflect the specific type of the objects in the collection. For example, if you have an ArrayList of Student objects, your from clause (C#) or From Clause (Visual Basic) should look like this:

// C#
var query = from Student s in arrList
'Visual Basic
Dim query = From student As Student In arrList 

By specifying the type of the range variable, you are casting each item in the ArrayList to a Student.

The use of an explicitly typed range variable in a query expression is equivalent to calling the Cast(Of TResult) method. Cast(Of TResult) throws an exception if the specified cast cannot be performed. Cast(Of TResult) and OfType(Of TResult) are the two Standard Query Operator methods that operate on non-generic IEnumerable types. In Visual Basic, you must explicitly call the Cast(Of TResult) method on the data source to ensure a specific range variable type. For more information, seeType Relationships in Query Operations (Visual Basic) and Type Relationships in LINQ Query Operations (C#).

The following example shows a simple query over an ArrayList. Note that this example uses object initializers when the code calls the Add method, but this is not a requirement.

Imports System.Collections
Imports System.Linq

Module Module1

    Public Class Student
        Public Property FirstName As String 
        Public Property LastName As String 
        Public Property Scores As Integer()
    End Class 

    Sub Main()

        Dim student1 As New Student With {.FirstName = "Svetlana", 
                                     .LastName = "Omelchenko", 
                                     .Scores = New Integer() {98, 92, 81, 60}}
        Dim student2 As New Student With {.FirstName = "Claire", 
                                    .LastName = "O'Donnell", 
                                    .Scores = New Integer() {75, 84, 91, 39}}
        Dim student3 As New Student With {.FirstName = "Cesar", 
                                    .LastName = "Garcia", 
                                    .Scores = New Integer() {97, 89, 85, 82}}
        Dim student4 As New Student With {.FirstName = "Sven", 
                                    .LastName = "Mortensen", 
                                    .Scores = New Integer() {88, 94, 65, 91}}

        Dim arrList As New ArrayList()

        ' Use an explicit type for non-generic collections 
        Dim query = From student As Student In arrList 
                    Where student.Scores(0) > 95 
                    Select student

        For Each student As Student In query
            Console.WriteLine(student.LastName & ": " & student.Scores(0))
        ' Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.")
    End Sub 

End Module 
' Output: 
'   Omelchenko: 98 
'   Garcia: 97
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft