In previous articles, I have mentioned how to Remove All WhiteSpace from string in C# (Multiple Methods) and how to Upload or Send File in Postman but now in this article, I have mentioned how to convert a list to dictionary in C# using various possible methods.
Table of Contents
Using Linq
One of the easy and quick ways to convert the list into the dictionary in C# is Using Lambda
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
//create list of students type
List<Students> studentList = new List<Students> {
new Students(){Id = 208, Name = "John"},
new Students(){Id = 209, Name = "Wick"},
new Students(){Id = 210, Name = "Morbius"},
new Students(){Id = 211, Name = "Geralt"}
};
//convert list into dictionary
// key as Id
//value as Name
var studentsById = studentList.ToDictionary(keySelector: m => m.Id, elementSelector: m => m.Name);
//loop dictionary
foreach(var stu in studentsById)
{
Console.WriteLine(stu.Value);
}
}
}
public class Students{
public int Id {get;set;}
public string Name {get;set;}
}
In the above code, we are first, creating "Student
" list with some values in it.
Then using .ToDictionary()
(Linq Method) to convert List Id/Name into Dictionary, key/value pairs and then looping Dictionary to print it's value.
Output:
John
Wick
Morbius
Geralt
Using For Loop
Instead of using Linq as shown above, if you want we can loop each list item and add them in a dictionary, like this
List<Students> studentList = new List<Students> {
new Students(){Id = 208, Name = "John"},
new Students(){Id = 209, Name = "Wick"},
new Students(){Id = 210, Name = "Morbius"},
new Students(){Id = 211, Name = "Geralt"}
};
var studentsDic = new Dictionary<int, string>();
foreach(var stu in studentList)
{
studentsDic.Add(stu.Id, stu.Name);
}
so if you want to see the complete Console application sample for the above code, it would be like
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<Students> studentList = new List<Students> {
new Students(){Id = 208, Name = "John"},
new Students(){Id = 209, Name = "Wick"},
new Students(){Id = 210, Name = "Morbius"},
new Students(){Id = 211, Name = "Geralt"}
};
var studentsDic = new Dictionary<int, string>();
foreach(var stu in studentList)
{
studentsDic.Add(stu.Id, stu.Name);
}
//loop dictionary
foreach(var stu in studentsDic)
{
Console.WriteLine(stu.Value);
}
}
}
public class Students{
public int Id {get;set;}
public string Name {get;set;}
}
Output is the same as above since we are printing Dictionary value.
John
Wick
Morbius
Geralt
Handling Duplicates while converting the list to Dictionary
One of the issues, while converting the list into the dictionary is that you can duplicate values in a list and Dictionary only allows adding unique items, otherwise, it will throw an error as mentioned below
System.ArgumentException: An item with the same key has already been added.
Here is the example of it in image
In the above image code, you can see we have repeated a Key/Value - 201/Morbius, and when adding it into the dictionary, it is throwing an error.
So to avoid this error, we can do something like this
List<Students> studentList = new List<Students> {
new Students(){Id = 208, Name = "John"},
new Students(){Id = 209, Name = "Wick"},
new Students(){Id = 210, Name = "Morbius"},
new Students(){Id = 211, Name = "Geralt"},
new Students(){Id = 210, Name = "Morbius"} // repeated value
};
var studentsDic = new Dictionary<int, string>();
var studentsById = studentList.GroupBy(m => m.Id)
.ToDictionary(keySelector: g => g.Key, elementSelector: g => g.First().Name);
//loop dictionary
foreach(var stu in studentsById)
{
Console.WriteLine(stu.Key + " "+stu.Value);
}
In the above code, we are first grouping elements by "Id" and then converting them into Dictionary.
Also, one of the things to note here, is that when we are using elementSelector
we are taking the first item's Name property from the list.
You may also like to read:
How to get file extension or file size in C# ? ( Code With Example )
Extract String between two string in C#?
Convert List to string in C# (Comma-separated, New-Line)
Convert String to List in C# and string array to list
How to read pdf file in C#? (Working example using iTextSharp)
Read file in C# (Text file .NET and .NET Core example)
Generate Class from XSD in C# (Using CMD or Visual Studio)