方法 : LINQ を使用して ArrayList を照会する

LINQ を使用して ArrayList などの非ジェネリックの IEnumerable コレクションを照会する場合、範囲変数の型を明示的に宣言して、オブジェクトの特定の型をコレクションに反映させる必要があります。 たとえば、Student オブジェクトの ArrayList を照会する場合、from 句 (C#) または From 句 (Visual Basic) は次のようになります。

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

範囲変数の型を指定することで、ArrayList 内の各項目が Student にキャストされます。

明示的に型指定された範囲変数をクエリ式で使用すると、Cast<TResult> メソッドを呼び出した場合と同じ結果を得ることができます。 指定されたキャストを実行できない場合、Cast<TResult> は例外をスローします。 Cast<TResult>OfType<TResult> は、非ジェネリックの IEnumerable 型に対して実行できる 2 つの標準クエリ演算子メソッドです。 Visual Basic では、範囲変数を確実に特定の型にするために、データ ソースで Cast<TResult> メソッドを明示的に呼び出す必要があります。 詳細については、「クエリ操作での型の関係 (Visual Basic)」および「LINQ クエリ操作での型の関係 (C#)」を参照してください。

使用例

ArrayList に対する単純なクエリの例を次に示します。 この例では、Add メソッドの呼び出し時にオブジェクト初期化子を使用していますが、これは必須ではありません。

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()
        arrList.Add(student1)
        arrList.Add(student2)
        arrList.Add(student3)
        arrList.Add(student4)

        ' 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))
        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
using System;
using System.Collections;
using System.Linq;

namespace NonGenericLINQ
{
    public class Student
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int[] Scores { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            ArrayList arrList = new ArrayList();
            arrList.Add(
                new Student
                    {
                        FirstName = "Svetlana", LastName = "Omelchenko", Scores = new int[] { 98, 92, 81, 60 }
                    });
            arrList.Add(
                new Student
                    {
                        FirstName = "Claire", LastName = "O’Donnell", Scores = new int[] { 75, 84, 91, 39 }
                    });
            arrList.Add(
                new Student
                    {
                        FirstName = "Sven", LastName = "Mortensen", Scores = new int[] { 88, 94, 65, 91 }
                    });
            arrList.Add(
                new Student
                    {
                        FirstName = "Cesar", LastName = "Garcia", Scores = new int[] { 97, 89, 85, 82 }
                    });

            var query = from Student student in arrList
                        where student.Scores[0] > 95
                        select student;

            foreach (Student s in query)
                Console.WriteLine(s.LastName + ": " + s.Scores[0]);

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}
/* Output: 
    Omelchenko: 98
    Garcia: 97
*/

参照

概念

LINQ to Objects