LINQ ToDictionary() Method

In LINQ, the ToDictionary operator is useful to convert list/collection (IEnumerable<T>) items to a new dictionary object (Dictionary<TKey, TValue>), and it will optimize the list/collection items by getting only required values.

Syntax of LINQ ToDictionary Operator

Following is the syntax of using the LINQ ToDictionary operator to convert the collection to a new dictionary object.

LINQ ToDictionary Syntax in C#

var student = objStudent.ToDictionary(x => x.Id, x => x.Name);

LINQ ToDictionary Syntax in VB.NET

Dim student = objStudent.ToDictionary(Function(x) x.Id, Function(x) x.Name)

If you observe the above syntax, we convert the “objStudent” collection to a dictionary object and get only the required field values (Id and Name).

LINQ ToDictionary Operator Example

Following is the example of using the LINQ ToDictionary operator to convert the collection to a 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 the “objStudent” collection to dictionary object and getting values from two fields (Id and Name)

Result of LINQ ToDictionary Operator Example

Following is the result of the 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 objects in c#, vb.net with example.