In this blog post, we will learn how we can convert a list to a dictionary in C#.

Let's say you have a list and you want to convert it to a dictionary so that you can have defined key value pairs so you can use them, this is where this conversion from a list to dictionary comes in handy.

 

 

 

 

Convert List To Dictionary Using .ToDictionary() Method In C#

In this method, we will use the .ToDictionary() method available to us on IEnumberable types.

As we know, lists are an implementation of IEnumberable type, we can use the .ToDictionary() method on lists.

 

Example:

namespace ConsoleApp { public class Program { public static void Main() { var listOfString = new List { "USA", "India", "Brazil", "UK", "Australia" }; var dictionary = listOfString.ToDictionary(keySelector: x => x.ToLower(), elementSelector: x => x); foreach (var item in dictionary) { Console.WriteLine($"Key: {item.Key}, Value: {item.Value}"); } } } }

 

Result:

Key: usa, Value: USA Key: india, Value: India Key: brazil, Value: Brazil Key: uk, Value: UK Key: australia, Value: Australia

 

In the above code example, we can see that we have a list of string available that we want to convert to a dictionary.

We use the .ToDictionary() method on the list which gives us a way to define the key and the value for the new dictionary we are about to create from the list.

These are lambda functions through which we can define the keySelector and elementSelector for the dictionary.

You can manipulate the values of the incoming value to your liking before you store it in the dictionary item.

 

 

Convert List Of Object To Dictionary Using .ToDictionary() Method In C#

In this example, we will use the same .ToDictionary() method but on a complex object this time.

We will use a list of class and convert that list to dictionary.

 

Example:

namespace ConsoleApp { public class Country { public int Id { get; set; } public string Name { get; set; } } public class Program { public static void Main() { var listOfCountry = new List { new() { Id = 1, Name = "USA"}, new() { Id = 2, Name = "India"}, new() { Id = 3, Name = "Australia"}, new() { Id = 4, Name = "Brazil"}, new() { Id = 5, Name = "UK"}, }; var dictionary = listOfCountry.ToDictionary(keySelector: x => x.Id, elementSelector: x => x.Name.ToUpper()); foreach (var item in dictionary) { Console.WriteLine($"Key: {item.Key}, Value: {item.Value}"); } } } }

 

Result:

Key: 1, Value: USA Key: 2, Value: INDIA Key: 3, Value: AUSTRALIA Key: 4, Value: BRAZIL Key: 5, Value: UK

 

In the above code example, we can see that we have a list of country type available that we want to convert to a dictionary.

This country class has 2 properties, an Id of type int, and a Name property of type string.

We use the .ToDictionary() method on the list which gives us a way to define the key and the value for the new dictionary we are about to create from the list.

These are lambda functions through which we can define the keySelector and elementSelector for the dictionary.

You can manipulate the values of the incoming value to your liking before you store it in the dictionary item.

We are using the Id property on the list item to store in the key and using the name property from the list item to store in the value of the dictionary item.