English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

LINQ 転換演算子

LINQのConversion演算子は、シーケンス(コレクション)内の要素のタイプを変換するために使用できます。変換演算子は3種類に分類されます:As演算子(AsEnumerableおよびAsQueryable),To演算子(ToArray、ToDictionary、ToListおよびToLookup)および変換演算子(CastおよびOfType)。

以下の表にすべての変換演算子が示されています。

メソッド説明
AsEnumerable

入力シーケンスをIEnumerable < T>として返します

AsQueryable

IEnumerableをIQueryableに変換して、リモートクエリプロバイダをシミュレートします

Cast

非汎型のコレクションを汎型のコレクション(IEnumerableからIEnumerable)に変換します

OfType指定されたタイプに基づいてコレクションをフィルタリングします
ToArrayコレクションを配列に変換します
ToDictionary

キーセレクターファンクションに基づいて要素をDictionaryに配置します

ToList

コレクションをListに変換します

ToLookup要素をLookup<TKey,TElement>にグループ化します

AsEnumerableとAsQueryableメソッド

AsEnumerableとAsQueryableメソッドはそれぞれ元のオブジェクトをIEnumerable <T>またはIQueryable <T>に変換または変換します。

以下の例を見てください:

class Program
{
    static void ReportTypeProperties<T>(T obj)
    {
        Console.WriteLine("Compile-time type: {0}", typeof(T).Name);
        Console.WriteLine("実際の型: {0}", obj.GetType().Name);
    }
    static void Main(string[] args)
    {
        Student[] studentArray = { 
                new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
                new Student() { StudentID = 2, StudentName = "Steve",  Age = 21 } ,
                new Student() { StudentID = 3, StudentName = "Bill",  Age = 25 } ,
                new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
                new Student() { StudentID = 5, StudentName = "Ron" , Age = 31 } ,
            };   
            
        ReportTypeProperties(studentArray);
        ReportTypeProperties(studentArray.AsEnumerable());
        ReportTypeProperties(studentArray.AsQueryable());   
    }
}
出力:
Compile-時間の型: Student[]
実際の型: Student[]
Compile-時間の型: IEnumerable`1
実際の型: Student[]
Compile-time type: IQueryable`1
実際のタイプ: EnumerableQuery`1

上の例のように、AsEnumerableとAsQueryableメソッドはそれぞれコンパイル時間のタイプをIEnumerableとIQueryableに変換します。

Cast

Castの作用はAsEnumerable<T>と同じです。それが元のオブジェクトをIEnumerable<T>に強制変換します。

class Program
{
    static void ReportTypeProperties<T>(T obj)
    {
        Console.WriteLine("Compile-time type: {0}", typeof(T).Name);
        Console.WriteLine("実際の型: {0}", obj.GetType().Name);
    }
    static void Main(string[] args)
    {
        Student[] studentArray = { 
                new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
                new Student() { StudentID = 2, StudentName = "Steve",  Age = 21 } ,
                new Student() { StudentID = 3, StudentName = "Bill",  Age = 25 } ,
                new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
                new Student() { StudentID = 5, StudentName = "Ron" , Age = 31 } ,
            };   
         
        ReportTypeProperties(studentArray);
        ReportTypeProperties(studentArray.Cast<Student>());
    }
}
出力:
Compile-時間の型: Student[]
実際の型: Student[]
Compile-時間の型: IEnumerable`1
実際の型: Student[]
Compile-時間の型: IEnumerable`1
実際の型: Student[]
Compile-時間の型: IEnumerable`1
実際の型: Student[]

studentArray.Cast<Student>() と (IEnumerable<Student>)studentArray は同じですが、Cast<Student>() は読みやすくなります。

To演算子:ToArray()、ToList()、ToDictionary()

その名の通りの、ToArray()、ToList()、ToDictionary()メソッドのソースオブジェクトの変換は、配列、リスト、辞書です。

To演算子はクエリを実行します。これにより、リモートクエリプロバイダーがクエリを実行し、基層データソース(SQL Serverデータベースなど)から結果を取得します。

IList<string> strList = new List<string>() { 
                                            "One", 
                                            "Two", 
                                            "Three", 
                                            "Four", 
                                            "Three" 
                                            };
string[] strArray = strList.ToArray<string>();// リストを配列に変換する
IList<string> list = strArray.ToList<string>(); // 配列をリストに変換する

ToDictionary - ジェネリックリストをジェネリック辞書に変換する:

IList<Student> studentList = new List<Student>() { 
                    new Student() { StudentID = 1, StudentName = "John", age = 18 } ,
                    new Student() { StudentID = 2, StudentName = "Steve",  age = 21 } ,
                    new Student() { StudentID = 3, StudentName = "Bill",  age = 18 } ,
                    new Student() { StudentID = 4, StudentName = "Ram" , age = 20 } ,
                    new Student() { StudentID = 5, StudentName = "Ron" , age = 21 } 
                };
//以下はリストを辞書に変換し、StudentIdがキーです。
IDictionary<int, Student> studentDict = 
                                studentList.ToDictionary<Student, int>(s => s.StudentID); 
foreach(var key in studentDict.Keys)
	Console.WriteLine("Key: {0}, Value: {1", 
                                key, (studentDict[key] as Student).StudentName);
出力:
Key: 1, Value: John
Key: 2, Value: Steve
Key: 3, Value: Bill
Key: 4, Value: Ram
Key: 5, Value: Ron

以下の図は、上記の例のstudentDictがどのようにkeyを含んでいるかを示しています。-valueのペア、keyはStudentID、valueはStudentオブジェクト。

LINQ-ToDictionary演算子