Key Value pairs in Collections


A new approach has evolved in Collections ,i.e The LookUp<TKey,TValue> is one the Class in the Collection which represents a collection of keys each mapped to one or more values.

We are very familiar with the Dictonary Collection which maps only one value per key mapping. But when we come across to map the keys with more values, then we have this LookUp<TKey,TValue>

Where

TKey    ----> The types of the keys in the LookUp<TKey,TValue>
TValue ---->    The type of the elements of each IEnumerable<T> value in the LookUp<TKey,TElement>.

I have worked out on this concept to understand the LookUp class. Before going in deeply, let's create a Custom Class like this

Employee.cs

using System.Collections.Generic;

/// <summary>
/// Summary description for Employee
/// </summary>
public class Employee
{
    
public int ID { get; set; }
    
public string Email { get; set; }
    
public int Age { get; set; }
    
public string Name { get; set; }
    
public bool MartialStatus { get; set; }
    
public Employee()
    {
         
//
        
// TODO: Add constructor logic here
        
//
    }
  }

I have taken an aspx page which I have designed like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LookUpDemo.aspx.cs" Inherits="LookUpDemo" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
<title>Demo on Lookup(TKey,TElement) Class</title>
    
<style type="text/css">
        .rbn
        {
            font-family: Verdana;
            font-size: small;
        }
     
</style>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:GridView ID="gv1" runat="server" BackColor="White" BorderColor="#CC9966" BorderStyle="None"
             
BorderWidth="1px" CellPadding="4">
            
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
            
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
            
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
            
<RowStyle BackColor="White" ForeColor="#330099" />
            
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
            
<SortedAscendingCellStyle BackColor="#FEFCEB" />
            
<SortedAscendingHeaderStyle BackColor="#AF0101" />
            
<SortedDescendingCellStyle BackColor="#F6F0C0" />
            
<SortedDescendingHeaderStyle BackColor="#7E0000" />
        
</asp:GridView>
        
<div class="rbn">
            
<label for="rbnList" style="font-weight: bold">
                Select the Martial Status(Checked Checkboxes are Married)
</label>
            
<asp:RadioButtonList ID="rbnList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="rbnList_SelectedIndexChanged">
                 
<asp:ListItem Text="Single" Value="false"></asp:ListItem>
                 
<asp:ListItem Text="Married" Value="true"></asp:ListItem>
            
</asp:RadioButtonList>
        
</div>
        
<br />
        
<asp:GridView ID="gv2" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
            
<AlternatingRowStyle BackColor="White" />
            
<EditRowStyle BackColor="#2461BF" />
            
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            
<RowStyle BackColor="#EFF3FB" />
            
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            
<SortedAscendingCellStyle BackColor="#F5F7FB" />
            
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
            
<SortedDescendingCellStyle BackColor="#E9EBEF" />
            
<SortedDescendingHeaderStyle BackColor="#4870BE" />
        
</asp:GridView>
    
</div>
    
</form>
</body>
</html>

Here in my example I have taken the Employee Details and I am showing the employee details in the first grid. Below that I have taken a radio button list which has two ListItems Called "SINGLE" and "MARRIED".

These ListItems "SINGLE" and "MARRIED" have values as "false" and "true". The LookUp has keys each mapped to the one or more values. So based on the key selected the values will be filtered and showed in the below gridview.

"Now, before defining a Lookup Collection, we need to know some basic fundamentals . Like Dictionary we can not create a Lookup Collection directly. While creating the instance of the Lookup collection object, we need to invoke an extension method called ToLookup() which will be returning a Lookup<Tkey, TElement> object. "

The following code snippet shows creation of a Lookup Collection with key type of Boolean with Employee Collection.image

Collections1.gif

The output for the two cases is given in the below screen shot

Collections2.gif

  1. Filtered based on the radio button("Married") selected

  2. Filtered based on the Radio Button("Single") Selected.

Collections3.gif

empList.ToLookup(c => c.MartialStatus) will pass Lookup<bool, Employee> to employeeLookup Collection. The Lookup collection is created on key type of Boolean. Hence we can access the elements as follows,

Happy Coding :-)

Up Next
    Ebook Download
    View all
    Learn
    View all