In LINQ, ToDictionary operator is used to convert list/collection (IEnumerable<T>) items to a new dictionary object (Dictionary<TKey,TValue>) and it will optimize list/collection items by getting only required values.
Following is the syntax of using LINQ ToDictionary operator to convert collection to new dictionary object.
C# Code
var student = objStudent.ToDictionary(x => x.Id, x => x.Name);
VB.NET Code
Dim student = objStudent.ToDictionary(Function(x) x.Id, Function(x) x.Name)
If you observe above syntax we are converting “objStudent” collection to dictionary object and getting only required field values (Id and Name).
Following is the example of using LINQ ToDictionary operator to convert the collection to new dictionary object.
C# Code
using System;
using System.Linq;
using System.Collections.Generic;
namespace LINQExamples
{
class Program
{
static void Main(string[] args)
{
List<Student> objStudent = new List<Student>()
{
new Student() { Id=1,Name = "Suresh Dasari", Gender = "Male",Location="Chennai" },
new Student() { Id=2,Name = "Rohini Alavala", Gender = "Female", Location="Chennai" },
new Student() { Id=3,Name = "Praveen Alavala", Gender = "Male",Location="Bangalore" },
new Student() { Id=4,Name = "Sateesh Alavala", Gender = "Male", Location ="Vizag"},
new Student() { Id=5,Name = "Madhav Sai", Gender = "Male", Location="Nagpur"}
};
var student = objStudent.ToDictionary(x => x.Id, x => x.Name);
foreach (var stud in student)
{
Console.WriteLine(stud.Key + "\t" + stud.Value);
}
Console.ReadLine();
}
}
class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string Location { get; set; }
}
}
VB.NET Code
Module Module1
Sub Main()
Dim objStudent As New List(Of Student)() From {
New Student() With {.Id = 1, .Name = "Suresh Dasari", .Gender = "Male", .Location = "Chennai"},
New Student() With {.Id = 2, .Name = "Rohini Alavala", .Gender = "Female", .Location = "Chennai"},
New Student() With {.Id = 3, .Name = "Praveen Alavala", .Gender = "Male", .Location = "Bangalore"},
New Student() With {.Id = 4, .Name = "Sateesh Alavala", .Gender = "Male", .Location = "Vizag"},
New Student() With {.Id = 5, .Name = "Madhav Sai", .Gender = "Male", .Location = "Nagpur"}
}
Dim student = objStudent.ToDictionary(Function(x) x.Id, Function(x) x.Name)
For Each stud In student
Console.WriteLine(Convert.ToString(stud.Key) + vbTab + stud.Value)
Next
Console.ReadLine()
End Sub
Class Student
Public Property Id() As Int32
Get
Return m_Id
End Get
Set(ByVal value As Int32)
m_Id = value
End Set
End Property
Private m_Id As Int32
Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
Private m_Name As String
Public Property Gender() As String
Get
Return m_Gender
End Get
Set(ByVal value As String)
m_Gender = value
End Set
End Property
Private m_Gender As String
Public Property Location() As String
Get
Return m_Location
End Get
Set(ByVal value As String)
m_Location = value
End Set
End Property
Private m_Location As String
End Class
End Module
If you observe the above example we are converting “objStudent” collection to dictionary object and getting values from two fields (Id and Name)
Following is the result of LINQ ToDictionary operator example.
1 Suresh Dasari
2 Rohini Alavala
3 Praveen Alavala
4 Sateesh Alavala
5 Madhav Sai
This is how we can use LINQ ToDictionary() method to convert list / collection items to new dictionary object in c#, vb.net with example.