How to convert a dictionary to a list in C#

By FoxLearn 2/26/2025 2:10:26 AM   943
In C#, dictionaries are commonly used to store key-value pairs, but sometimes, you might want to work with a list of these pairs instead of a dictionary.

C# Dictionary to List

Converting a dictionary to a list is a common task, and C# provides a few simple ways to achieve this using LINQ or loops.

Convert a Dictionary to a List of KeyValuePair in C#

The simplest way to convert a dictionary to a list is by using the LINQ ToList() method. This method will convert each key-value pair into a KeyValuePair<TKey, TValue> in a list.

For example, c# convert dictionary to list

// c# dictionary to list of key value pairs
var dictionary = new Dictionary<int, string>()
{
    [100] = "Bob",
    [101] = "Tom",
    [102] = "Teddy"
};

// convert dictionary to list c#
var list = dictionary.ToList(); // c# dictionary tolist

foreach(var kvp in list)
{
    Console.WriteLine($"{kvp.Key}={kvp.Value}");
}

Output:

100=Bob
101=Tom
102=Teddy

When used on a dictionary, the ToList() method returns a list of KeyValuePair objects, where each KeyValuePair contains a key and its corresponding value from the dictionary.

Convert Dictionary Keys to a List in C#

If you only need the keys from the dictionary, you can use ToList() on the Dictionary.Keys collection.

For example, c# dictionary keys to list

// c# dictionary keys to list
var list = dictionary.Keys.ToList();
foreach(var key in list)
{
    Console.WriteLine(key);
}

Output:

100
101
102

Convert Dictionary Values to a List in C#

If you are only interested in the values of the dictionary, you can use ToList() on the Dictionary.Values collection to convert dictionary values to a list in C#.

For example, c# dictionary to list of values

// c# dictionary to list of values 
var list = dictionary.Values.ToList();
foreach(var val in list)
{
    Console.WriteLine(val);
}

Output:

Bob
Tom
Teddy

Convert Dictionary to a List of Tuples in C#

To convert a dictionary to a list of tuples, you can use LINQ's Select() method to project each key-value pair as a tuple. The result can then be converted to a list using ToList().

For example, c# dictionary to list linq

// c# dictionary to list linq
var list = dictionary.Select(kvp => (Id: kvp.Key, Name: kvp.Value)).ToList();

foreach(var tuple in list)
{
    Console.WriteLine($"{tuple.Name} has id {tuple.Id}");
}

Output:

Bob has id 100
Tom has id 101
Teddy has id 102

For example, c# list to dictionary linq

Suppose you have a list of objects, and you want to convert it to a dictionary where one property is the key and another property is the value.

public class Player
{
    public string Name { get; set; }
    public int Level { get; set; }
}

List<Player> players = new List<Player>
{
    new Player { Name = "ShadowMaster", Level = 78 },
    new Player { Name = "DragonSlayer22", Level = 92 },
    new Player { Name = "StealthNinja", Level = 64 }
};

// Using LINQ to convert List to Dictionary
Dictionary<string, int> playerLevels = players.ToDictionary(p => p.Name, p => p.Level);

// Output the dictionary
foreach (var player in playerLevels)
{
    Console.WriteLine($"{player.Key}: {player.Value}");
}

Using a Loop to Convert Dictionary to a List in C#

Sometimes you might want more control over how the dictionary is converted to a list. You can manually loop through the dictionary and add items to a list as needed.

var list = new List<KeyValuePair<int, string>>();

foreach(var kvp in dictionary)
{
    if (kvp.Key > 100)
    {
        list.Add(kvp);
    }
}
Console.WriteLine($"List has {list.Count} items");

Output:

List has 2 items

How to get all keys of a dictionary with C#?

The Keys property of a dictionary returns a collection containing all the keys. It gives you a KeyCollection object, which you can iterate over to access the dictionary keys.

// Create a dictionary
Dictionary<string, int> countryList = new Dictionary<string, int>();
countryList.Add("USA", 331);
countryList.Add("India", 1380);
countryList.Add("China", 1441);
countryList.Add("Japan", 125);
countryList.Add("Germany", 83);

// Get and display keys
Dictionary<string, int>.KeyCollection keys = countryList.Keys;

foreach (string key in keys)
{
     Console.WriteLine("Country: {0}", key);
}

Output:

Country: USA
Country: India
Country: China
Country: Japan
Country: Germany

In this example:

  • The Keys property of CountryList retrieves the collection of country names (the dictionary keys).
  • A foreach loop is used to iterate through the KeyCollection and print each country name.

C# KeyCollection to List

To convert a KeyCollection (which is typically a collection of keys from a dictionary) to a List in C#, you can use the ToList() extension method provided by LINQ.

For example, c# dictionary to list of objects

// Create a dictionary
Dictionary<int, string> myDictionary = new Dictionary<int, string>
{
     { 1, "One" },
     { 2, "Two" },
     { 3, "Three" }
};

// Get all the keys of the dictionary
var keys = myDictionary.Keys;

// Print all the keys
foreach (var key in keys)
{
     Console.WriteLine(key);
}

Output:

1
2
3

In this example:

  • myDictionary.Keys retrieves the KeyCollection containing all the keys in the dictionary.
  • You can iterate through the keys using a foreach loop to access and print them.

You can also convert the keys to a List if you need to work with them as a list:

List<int> keyList = myDictionary.Keys.ToList();

This will give you the keys in a List<int>.

C# Convert Dictionary Values to List

To convert the values of a Dictionary to a List in C#, you can use the Values property of the dictionary, which returns a ValueCollection. Then, you can convert this collection into a List using the ToList() method.

For example, convert dictionary values to list in C#.

// Create a dictionary
Dictionary<string, int> countryPopulation = new Dictionary<string, int>();
countryPopulation.Add("USA", 331);
countryPopulation.Add("India", 1380);
countryPopulation.Add("China", 1441);
countryPopulation.Add("Japan", 125);
countryPopulation.Add("Germany", 83);

// Convert dictionary values to a List
List<int> populationList = countryPopulation.Values.ToList();

// Print all the values (population)
foreach (int population in populationList)
{
     Console.WriteLine("Population: {0} million", population);
}

Output:

Population: 331 million
Population: 1380 million
Population: 1441 million
Population: 125 million
Population: 83 million

In this example:

  • countryPopulation.Values retrieves the collection of values (population counts in this case) from the dictionary.
  • ToList() converts the ValueCollection into a List<int>.
  • You can now work with the population values as a List.