.Net(VB C#) LINQ
Enumerable.Cast メソッド、OfType メソッド

Enumerable.Castメソッド
【C#】
IEnumerable の要素を、指定した型にキャストします。
public static IEnumerable<TResult> Cast<TResult>(this IEnumerable source)
【VB】
IEnumerable の要素を、指定した型にキャストします。
<ExtensionAttribute>
Public Shared Function Cast(Of TResult) (source As IEnumerable) As IEnumerable(Of TResult)

Enumerable.OfTypeメソッド
【C#】
指定された型に基づいて IEnumerable の要素をフィルター処理します。
public static IEnumerable OfType(this IEnumerable source)
【VB】
指定された型に基づいて IEnumerable の要素をフィルター処理します。
<ExtensionAttribute>
Public Shared Function OfType(Of TResult) (source As IEnumerable) As IEnumerable(Of TResult)

シーケンス(配列やコレクションなど)の要素を指定した型にキャストし、その型のシーケンスを返します。
Castメソッドは要素が指定した型にキャストできないときに例外をスローします。
OfTypeメソッドは要素が指定した型にキャストできるもののみを抽出します。

ArrayListなど非ジェネリックなコレクションを、指定した型のIEnumerableにできるので、
Linqを利用できないコレクションをLinqで操作したいときに利用します。

このメソッドは遅延実行されます。


テストデータです。
親クラスと、親クラスを継承した子クラスを作成します。
親クラスのみを格納したArrayListと
親クラスと子クラスをごちゃ混ぜに格納したArrayListを作成します。
【C#】
//親クラスの定義
private class ParentClass
{
    public string ParentName { set; get; }
}
//子クラスの定義
private class ChildClass : ParentClass
{
    public string ChildName { set; get; }
}


//親クラスのみを格納したArrayListを作成
var aryOnlyParent = new System.Collections.ArrayList() 
    { 
        new ParentClass() {ParentName = "parent1"},
        new ParentClass() {ParentName = "parent2"},
        new ParentClass() {ParentName = "parent3"}
    };
//親クラスと子クラスをゴチャ混ぜに格納したArrayListを作成
var aryMix = new System.Collections.ArrayList()
    {
        new ParentClass() {ParentName = "parent1"},
        new ChildClass() {ParentName = "parent2", ChildName="child1"},
        null
    };
【VB】
'親クラスの定義
Private Class ParentClass
    Public Property ParentName As String
End Class
'子クラスの定義
Private Class ChildClass : Inherits ParentClass
    Public Property ChildName As String
End Class

'親クラスのみを格納したArrayListを作成
Dim aryOnlyParent = New System.Collections.ArrayList() From
        {
            New ParentClass() With {.ParentName = "parent1"},
            New ParentClass() With {.ParentName = "parent2"},
            New ParentClass() With {.ParentName = "parent3"}
        }
'親クラスと子クラスをゴチャ混ぜに格納したArrayListを作成
Dim aryMix = New System.Collections.ArrayList() From
        {
            New ParentClass() With {.ParentName = "parent1"},
            New ChildClass() With {.ParentName = "parent2", .ChildName = "child1"},
            Nothing
        }
親クラスのみを格納したArrayListの要素を、Castメソッドを使用して親クラスに変換します。
特に問題なく実行できます。
【C#】
//Cast<ParentClass>メソッドで、ArrayListの要素をParentClassにキャストする。
//すべてParentClassのArrayListであれば実行できる
var castList1 = aryOnlyParent.Cast<ParentClass>();

//出力
foreach (var itm in castList1)
    Console.WriteLine(itm.ParentName);
//parent1
//parent2
//parent3
【VB】
'Cast(Of ParentClass)メソッドで、ArrayListの要素をParentClassにキャストする。
'すべてParentClassのArrayListであれば実行できる
Dim castList1 = aryOnlyParent.Cast(Of ParentClass)()
'出力
For Each itm In castList1
    Console.WriteLine(itm.ParentName)
Next
'parent1
'parent2
'parent3
次に親クラス子クラスをゴチャ混ぜに格納したArrayListの要素を、Castメソッドを使用して子クラスに変換します。
子クラスに変換できないものがあるので例外がスローされます。
【C#】
//Cast<ChildClass>メソッドで、ArrayListの要素をChildClassにキャストする。
//ParentClassとChildClassのごちゃ混ぜArrayListは例外がスローされる
var castList2 = aryMix.Cast<ChildClass>();

//出力
foreach (var itm in castList2)
    Console.WriteLine(itm.ParentName);
//ParentClassとnullはChildClassにキャストできないので例外をスローする
【VB】
'Cast(Of ChildClass)メソッドで、ArrayListの要素をChildClassにキャストする。
'ParentClassとChildClassのごちゃ混ぜArrayListは例外がスローされる
Dim castList2 = aryMix.Cast(Of ChildClass)()

'出力
For Each itm In castList2
    Console.WriteLine(itm.ParentName)
Next
'ChildClassとnullはChildClassにキャストできないので例外をスローする
親クラス子クラスをゴチャ混ぜに格納したArrayListの要素を、TypeOfメソッドを使用して子クラスに変換します。
子クラスに変換できないものは除外されます。
【C#】
//ParentClassとChildClassのごちゃ混ぜArrayListを
var oftypeList = aryMix.OfType<ChildClass>();

//出力
foreach (var itm in oftypeList)
    Console.WriteLine(itm.ParentName);
//parent2
【VB】
'ParentClassとChildClassのごちゃ混ぜArrayListを
Dim oftypeList = aryMix.OfType(Of ChildClass)()

'出力
For Each itm In oftypeList
    Console.WriteLine(itm.ParentName)
Next
'parent2
※VBの場合、コンパイルオプションで「Option Infer」をONにし、型推論を有効にしてください。

.Net(VB C#) LINQのメソッド一覧

0 件のコメント: