C# で IEnumerable をリストに変換する
- 
          
            C#でToList()を使用してデータを IEnumerable からリストに変換する
- 
          
            C#でToList()を使用してデータを配列からリストに変換する
- 
          
            C#でToArray()を使用してデータをリストから配列に変換する
- 
          
            C#でAsEnumerable()を使用してデータをリストから IEnumerable に変換する
 
この記事では、データを IEnumerable から C# のリストに変換する方法について説明します。
C# で ToList() を使用してデータを IEnumerable からリストに変換する
    
IEnumerable は、System.Collections.Generic 名前空間に含まれるインターフェイスです。他のすべてのインターフェースと同様に、メソッドを公開します。
このケースでは、enumerator メソッドを公開します。このメソッドは、LINQ クエリや配列などのジェネリックリストと非ジェネリックリストの反復またはループをサポートします。
IEnumerable には、IEnumerator オブジェクトを返す GetEnumerator メソッドのみが含まれています。
public interface IEnumerable<out T> : System.Collections.IEnumerable
IEnumerable インターフェイスによって返される値は読み取り専用です。これは、操作がこれらのデータに限定されていることを意味します。
C# の ToList() メソッドは、これらのデータを操作するための代替メソッドです。C# のリスト内の要素は、追加、削除、順序付け、および再配置できます。
IEnumerable 値と比較して、リストで実行できる操作は非常に多くあります。
C#List クラスは、インデックスでアクセスできる強く型付けされたオブジェクトのコレクションを表します。ToList() 関数またはメソッドは、System.Linq 名前空間(言語-統合クエリ)にあります。
LINQ の ToList 演算子は、指定されたソースから要素を取得し、新しいリストを返します。入力はタイプリストに変換されます。
ToList() メソッドは、文字列インスタンスのリストを返します。ToList() 関数は、配列参照または IEnumerable 値で呼び出すことができ、その逆も可能です。
詳細については、このリファレンスを参照してください。
リスト要素には、次のようにアクセスまたは操作できます。
// an array of 4 strings
string[] animals = { "Cow", "Camel", "Elephant" };
// creating a new instance of a list
List<string> animalsList = new List<string>();
// use AddRange to add elements
animalsList.AddRange(animals);
// declaring a list and passing array elements into the list
List<string> animalsList = new List<string>(animals);
// Adding an element to the collection
animalsList.Add("Goat");
Console.WriteLine(animalsList[2]);  // Output Elephant, Accessing elements of a list
// Collection of new animals
string[] newAnimals = { "Sheep", "Bull", "Camel" };
// Insert array at position 3
animalsList.InsertRange(3, newAnimals);
// delete 2 elements at starting with element at position 3
animalsList.RemoveRange(3, 2);
次のように ToList 関数を使用するには、名前空間をインポートする必要があります。
using System.Collections.Generic;
using System.Linq;
以下は、ToList() メソッドの構文です。
List<string> result = countries.ToList();
以下は、IEnumerable を C# のリストに変換するためのサンプルコードです。
using System;
using System.Collections.Generic;
using System.Linq;
namespace TestDomain {
  class Program {
    public static void Main(String[] args) {
      IEnumerable<int> testValues = from value in Enumerable.Range(1, 10) select value;
      List<int> result = testValues.ToList();
      foreach (int a in result) {
        Console.WriteLine(a);
      }
    }
  }
}
出力:
1
2
3
4
5
6
7
8
9
10
C# で ToList() を使用してデータを配列からリストに変換する
以下は、C# で配列をリストに変換するためのサンプルコードです。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ArrayToListApp {
  class Program {
    static void Main(string[] args) {
      // create an array of African countries of type string containing the collection of data
      string[] countries = { "Nigeria",    "Ghana",   "Egypt",  "Liberia",
                             "The Gambia", "Morocco", "Senegal" };
      // countries.ToList() convert the data collection into the list.
      List<string> result = countries.ToList();
      // foreach loop is used to print the countries
      foreach (string s in result) {
        Console.WriteLine(s);
      }
    }
  }
}
出力:
Nigeria
Ghana
Egypt
Liberia
The Gambia
Morocco
Senegal
C# で ToArray() を使用してデータをリストから配列に変換する
以下は、C# でリストから配列に変換するコードの例です。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ListToArrayApp {
  class Program {
    static void Main(string[] args) {
      // create an array of African countries of type string containing the collection of data
      IEnumerable<int> testValues = from value in Enumerable.Range(1, 10) select value;
      // countries.ToList() convert the data collection into the list.
      List<int> result = testValues.ToList();
      int[] array = result.ToArray();  // convert string to array.
      // foreach loop is used to print the countries
      foreach (int i in array) {
        Console.WriteLine(i);
      }
    }
  }
}
出力:
1
2
3
4
5
6
7
8
9
10
C# で AsEnumerable() を使用してデータをリストから IEnumerable に変換する
以下は、IEnumerable をリストに変換して IEnumerable に戻すコードの例です。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ListToArrayApp {
  class Program {
    static void Main(string[] args) {
      List<int> list = new List<int>();
      IEnumerable enumerable = Enumerable.Range(1, 5);
      foreach (int item in enumerable) {
        list.Add(item);
      }
      Console.WriteLine("Output as List");
      foreach (var item in list) {
        Console.WriteLine(item);
      }
      Console.WriteLine("Output as Enumerable");
      // using the AsEnumerable to	convert list back to Enumerable
      IEnumerable resultAsEnumerable = list.AsEnumerable();
      foreach (var item in resultAsEnumerable) {
        Console.WriteLine(item);
      }
    }
  }
}
出力:
Output as List
1
2
3
4
5
Output as Enumerable
1
2
3
4
5
