How to: Query an ArrayList with LINQ
This page is specific to:.NET Framework Version:3.54.0
Language-Integrated Query (LINQ)
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.

Example

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 FirstName As String
        Public LastName As String
        Public 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()
        arrList.Add(student1)
        arrList.Add(student2)
        arrList.Add(student3)
        arrList.Add(student4)

        ' Use an explicit type for non-generic collections
        ' Use an explicit type for non-generic collections
        Dim query = From s In arrList _
                    Let s1 As Student = CType(s, Student) _
                    Where s1.Scores(0) > 95 _
                    Select s1

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

End Module
' Output:
'   Omelchenko: 98
'   Garcia: 97


See Also

Concepts

Community Content

inline processing with select queries
Added by:iamdacian
is there any way i can have some processing of the element (student) at the select time, so i can do it inline at the select time instead having another foreach? something like

            var query = from Student student in arrList
where student.Scores[0] > 95
select (student, student) => (student; Console.WriteLine(student.LastName));

two parameters for the lambda expression because one is the input and the second is the output that the select needs...



© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View