How to convert a dictionary to a list in C#
By FoxLearn 1/22/2025 9:08:28 AM 322
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(); 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.
// 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#.
// 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
- Serial Port Communication in C#
- How to Optimize StringBuilder Performance in C#
- How to launch browser and visit web site in C#
- How to Convert an Object to a Byte Array in C#
- How to get project file path in C#
- How to pass anonymous types as parameters in C#
- How to fix 'Failure sending mail' in C#
- How to Parse a Comma-Separated String from App.config in C#