Add and retrieve Key Value from Generic List Collection in C# and VB.Net

Last Reply 11 months ago By pandeyism

Posted 11 months ago

i tried to do i am able to add dictionary to List.

But how we can fetch key from dictionary using for loop.

       private void Form1_Load(object sender, EventArgs e) {
           List<Employee> employees = new List<Employee>();
           try {
               Employee emp = new Employee();
               EmployeeDepartments dept = new EmployeeDepartments(); 
               for(int i = 0; i < 3; i++) {
                   emp.Name = "a"; 
                   Dictionary<string, string> dict = new Dictionary<string, string>();
                   dict.Add("Department", "IT");
                   dict.Add("Mark", "100");
                   emp.EmployeeDetails1 = dict;
                   employees.Add(emp);
               } 
               for(int i = 0; i < employees.Count; i++) { 
                   string name = employees[0].Name;
                   string Key =??;
                   string value =??;
                   // now i want here department as key and IT as value. 
               } 
           } catch(Exception ex) { 
               MessageBox.Show(ex.Message);
           } 
       }
Posted 11 months ago Modified on 11 months ago

Hi yogesjoshi,

Please refer below sample code.

Code

C#

public class Employee
{
    public string Name { get; set; }
    public Dictionary<string, string> EmployeeDetails { get; set; }
}

private void Form1_Load(object sender, EventArgs e)
{
    List<Employee> employees = new List<Employee>();
    try
    {
        for (int i = 0; i < 2; i++)
        {
            Employee emp = new Employee();
            emp.Name = "a";
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("Department", "IT");
            dict.Add("Mark", "100");
            emp.EmployeeDetails = dict;
            employees.Add(emp);
        }
        string message = "";
        for (int i = 0; i < employees.Count; i++)
        {
            Dictionary<string, string> dic = employees[i].EmployeeDetails;
            foreach (KeyValuePair<string, string> keyValuePair in dic)
            {
                string name = employees[i].Name;
                string key = keyValuePair.Key.ToString();
                string value = keyValuePair.Value.ToString();
                message += "Name = " + name + " | Key = " + key + " | Value = " + value + Environment.NewLine;
            }
        }
        MessageBox.Show(message);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

VB.Net

Public Class Employee
    Public Property Name As String
    Public Property EmployeeDetails As Dictionary(Of String, String)
End Class

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    Dim employees As List(Of Employee) = New List(Of Employee)()

    Try

        For i As Integer = 0 To 2 - 1
            Dim emp As Employee = New Employee()
            emp.Name = "a"
            Dim dict As Dictionary(Of String, String) = New Dictionary(Of String, String)()
            dict.Add("Department", "IT")
            dict.Add("Mark", "100")
            emp.EmployeeDetails = dict
            employees.Add(emp)
        Next

        Dim message As String = ""
        For i As Integer = 0 To employees.Count - 1
            Dim dic As Dictionary(Of String, String) = employees(i).EmployeeDetails
            For Each keyValuePair As KeyValuePair(Of String, String) In dic
                Dim name As String = employees(i).Name
                Dim key As String = keyValuePair.Key.ToString()
                Dim value As String = keyValuePair.Value.ToString()
                message += "Name = " & name & " | Key = " & key & " | Value = " & value & Environment.NewLine
            Next
        Next
        MessageBox.Show(message)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

Screenshot