LINQ ToDictionary() Method

  Total Views : 211
  Comments : 0
 
  Keywords : How to use linq todictionary() operator in c# / vb.net with example, Linq todictionary operator to convert list / collection to dictionary object in c# / vb.net with example

Here we will learn LINQ ToDictionary() method in c#, vb.net with example and how to use use LINQ ToDictionary() method to convert list / collection items to new dictionary object with example in c#, vb.net.

LINQ ToDictionary() Method / Operator

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

Syntax of LINQ ToDictionary Operator

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).

Example of LINQ ToDictionary Operator

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

Output of LINQ ToDictionary Operator Example

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.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User