[C#]Dictionary(連想配列)をリストに変換するには?

C#

どうも、ちょげ(@chogetarou)です。

Dictionary(連想配列)をリストに変換する方法を紹介します。

スポンサーリンク

要素をKeyValuePairのリストに変換

インターフェース, インターネット, プログラム, ブラウザ, Www

Dictionary(連想配列)の要素をKeyValuePairのリストに変換する方法は、3つあります。

List<T>()

1つ目は、List<T>()を使う方法です。

まず、「new List<T>()」を記述します。

そして、List<T>()の引数にDictionaryを指定します。

List<KeyValuePair<TKey, TValue>> items = new List<KeyValuePair<TKey, TValue>>(myDict);

上記のList<T>()は、引数に指定したDictionary(連想配列)をKeyValuePairのリストに変換します。

使用例

using System;
using System.Collections.Generic;
public class Example
{
    public static void Main()
    {
	    Dictionary<string, int> numbers = new Dictionary<string, int>()
	    {
	        { "one", 1 },
	        { "two", 2 },
	        { "three", 3 },
	        { "four", 4 },
	        { "five", 5 },
	    };
	    
	    List<KeyValuePair<string, int>> items = new List<KeyValuePair<string, int>>(numbers);
	    
	    Console.WriteLine(String.Join(",", items)); //[one, 1],[two, 2],[three, 3],[four, 4],[five, 5]
	}
}

ToList()

2つ目は、ToList()を使う方法です。

まず、System.Linqを導入します。

using System.Linq;

そして、DictionaryからToList()を呼び出します。

List<KeyValuePair<TKey, TValue>> items = myDict.ToList();

上記のToList()は、呼び出したDictionary(連想配列)の要素をKeyValuePairのリストに変換します。

使用例

using System;
using System.Collections.Generic;
using System.Linq;
public class Example
{
    public static void Main()
    {
	    Dictionary<string, int> numbers = new Dictionary<string, int>()
	    {
	        { "one", 1 },
	        { "two", 2 },
	        { "three", 3 },
	        { "four", 4 },
	        { "five", 5 },
	    };
	    
	    List<KeyValuePair<string, int>> items = numbers.ToList();
	    
	    Console.WriteLine(String.Join(",", items)); //[one, 1],[two, 2],[three, 3],[four, 4],[five, 5]
	}
}

foreachループ

3つ目は、foreachループを使う方法です。

まず、リストを用意します。

List<KeyValuePair<TKey, TValue>> items = new List<KeyValuePair<TKey, TValue>>();

Dictionaryをforeachでループします。

foreachのループ処理で、リストからAdd()メソッドを呼び出します。

そして、 Add()メソッドの引数にリストの要素を指定します。

foreach (var item in myDict)
{
    items.Add(item);
}

上記のforeachループは、ループしたDictionary(連想配列)の要素を用意したリストに格納します。

また、ここまでの処理で、Dictionaryの要素がKeyValuePairのリストに変換されます。

使用例

using System;
using System.Collections.Generic;
public class Example
{
    public static void Main()
    {
	    Dictionary<string, int> numbers = new Dictionary<string, int>()
	    {
	        { "one", 1 },
	        { "two", 2 },
	        { "three", 3 },
	        { "four", 4 },
	        { "five", 5 },
	    };
	    
	    List<KeyValuePair<string, int>> items = new List<KeyValuePair<string, int>>();
	    
	    foreach (var item in numbers)
	    {
	        items.Add(item);
	    }
	    
	    Console.WriteLine(String.Join(",", items)); //[one, 1],[two, 2],[three, 3],[four, 4],[five, 5]
	}
}
スポンサーリンク

キー(Key)をリストに変換

Dictionary(連想配列)のキーをリストに変換する方法は、3つあります。

Keys + List<T>()

1つ目は、KeysとList<T>を使う方法です。

まず、「new List<T>()」を記述します。

そして、List<T>()の引数で、DictionaryのKeysプロパティにアクセスします。

List<T> keys = new List<T>(myDict.Keys);

上記のList<T>()は、KeysプロパティにアクセスしたDictionary(連想配列)のキーをリストに変換します。

使用例

using System;
using System.Collections.Generic;
public class Example
{
    public static void Main()
    {
	    Dictionary<string, int> numbers = new Dictionary<string, int>()
	    {
	        { "one", 1 },
	        { "two", 2 },
	        { "three", 3 },
	        { "four", 4 },
	        { "five", 5 },
	    };
	    
	    List<string> keys = new List<string>(numbers.Keys);
	    
	    Console.WriteLine(String.Join(",", keys));//one,two,three,four,five
	}
}

Keys + ToList()

2つ目は、KeysとToList()を使う方法です。

まず、System.Linqを導入します。

using System.Linq;

Dictionary()のKeysプロパティにアクセスします。

そして、DictionaryのKeysプロパティからToList()を呼び出します。

List<T> keys = dict.Keys.ToList();

上記のToList()は、Dictionary(連想配列)のキーを配列に変換します。

使用例

using System;
using System.Linq;
using System.Collections.Generic;
public class Example
{
    public static void Main()
    {
	    Dictionary<string, int> numbers = new Dictionary<string, int>()
	    {
	        { "one", 1 },
	        { "two", 2 },
	        { "three", 3 },
	        { "four", 4 },
	        { "five", 5 },
	    };
	    
	    List<string> keys = numbers.Keys.ToList();
	    
	    Console.WriteLine(String.Join(",", keys));//one,two,three,four,five
	}
}

Select() + ToList()

3つ目は、Select()とToList()を使う方法です。

まず、System.Linqを導入します。

using System.Linq;

次に、DictionaryからSelect()を呼び出します。

Select()の引数に、引数のkeyプロパティを返すラムダ式を指定します。

そして、Select()からToList()を呼び出します。

List<T> keys = dict.Select(item => item.Key).ToList();

上記のdict.Select.ToList()は、Select()を呼び出したDictionary(連想配列)のキーをリストに変換します。

使用例

using System;
using System.Linq;
using System.Collections.Generic;
public class Example
{
    public static void Main()
    {
	    Dictionary<string, int> numbers = new Dictionary<string, int>()
	    {
	        { "one", 1 },
	        { "two", 2 },
	        { "three", 3 },
	        { "four", 4 },
	        { "five", 5 },
	    };
	    
	    List<string> keys = numbers.Select(item => item.Key).ToList();
	    
	    Console.WriteLine(String.Join(",", keys));//one,two,three,four,five
	}
}
スポンサーリンク

値(Value)をリストに変換

Dictionary(連想配列)のValue(値)をリスト(List)に変換する方法は、3つあります。

Values + List<T>()

1つ目は、ValuesとList<T>を使う方法です。

まず、「new List<T>()」を記述します。

そして、List<T>()の引数で、DictionaryのValuesプロパティにアクセスします。

List<T> keys = new List<T>(myDict.Values);

上記のList<T>()は、ValuesプロパティにアクセスしたDictionary(連想配列)の値をリストに変換します。

使用例

using System;
using System.Collections.Generic;
public class Example
{
    public static void Main()
    {
	    Dictionary<string, int> numbers = new Dictionary<string, int>()
	    {
	        { "one", 1 },
	        { "two", 2 },
	        { "three", 3 },
	        { "four", 4 },
	        { "five", 5 },
	    };
	    
	    List<int> values = new List<int>(numbers.Values);
	    
	    Console.WriteLine(String.Join(",", values));//1,2,3,4,5
	}
}

Values + ToList()

2つ目は、ValuesとToList()を使う方法です。

まず、System.Linqを導入します。

using System.Linq;

Dictionary()のValuesプロパティにアクセスします。

そして、DictionaryのValuesプロパティからToList()を呼び出します。

List<T> values = dict.Values.ToList();

上記のToList()は、Dictionary(連想配列)の値をリストに変換します。

使用例

using System;
using System.Linq;
using System.Collections.Generic;
public class Example
{
    public static void Main()
    {
	    Dictionary<string, int> numbers = new Dictionary<string, int>()
	    {
	        { "one", 1 },
	        { "two", 2 },
	        { "three", 3 },
	        { "four", 4 },
	        { "five", 5 },
	    };
	    
	    List<int> values = numbers.Values.ToList();
	    
	    foreach (var value in values)
	    {
	        Console.WriteLine(value);
	    }
	}
}

Select() + ToList()

3つ目は、Select()とToList()を使う方法です。

まず、System.Linqを導入します。

using System.Linq;

次に、DictionaryからSelect()を呼び出します。

Select()の引数に、引数のValueプロパティを返すラムダ式を指定します。

そして、Select()からToList()を呼び出します。

List<T> values = dict.Select(item => item.Value).ToList();

上記のdict.Select.ToList()は、Select()を呼び出したDictionary(連想配列)の値をリストに変換します。

使用例

using System;
using System.Linq;
using System.Collections.Generic;
public class Example
{
    public static void Main()
    {
	    Dictionary<string, int> numbers = new Dictionary<string, int>()
	    {
	        { "one", 1 },
	        { "two", 2 },
	        { "three", 3 },
	        { "four", 4 },
	        { "five", 5 },
	    };
	    
	    List<int> values = numbers.Select(item => item.Value).ToList();
	    
	    Console.WriteLine(String.Join(",", values));//1,2,3,4,5
	}
}
スポンサーリンク

まとめ

Dictionary(連想配列)の要素をKeyValuePairのリストに変換する方法は、次の3つです。

  • List<T>()を使う方法
    List<KeyValuePair<TKey, TValue>> items = new List<KeyValuePair<TKey, TValue>>(myDict);
  • ToList()を使う方法
    List<KeyValuePair<TKey, TValue>> items = myDict.ToList();
  • foreachループを使う方法
    List<KeyValuePair<TKey, TValue>> items = new List<KeyValuePair<TKey, TValue>>();
    foreach (var item in myDict) { items.Add(item); }

Dictionary(連想配列)のキーをリストに変換する方法は、次の3つです。

  • KeysとList<T>()を使う方法
    List<T> keys = new List<T>(myDict.Keys);
  • KeysとToList()を使う方法
    List<T> keys = dict.Keys.ToList();
  • Select()とToList()を使う方法
    List<T> keys = dict.Select(item => item.Key).ToList();

Dictionary(連想配列)の値(value)をリスト(List)に変換する方法は、次の3つです。

  • ValuesとList<T>()を使う方法
    List<T> values = new List<T>(myDict.Values);
  • ValuesとToList()を使う方法
    List<T> values = dict.Values.ToList();
  • Select()とToList()を使う方法
    List<T> values = dict.Select(item => item.Value).ToList();

コメント

タイトルとURLをコピーしました