A New Approach To .NET Key Value pairs in Collections

A new approach has been evolved in the Collections ,i.e The LookUp 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
Where
TKey    ----> The types of the keys in the LookUp
TValue ---->    The type of the elements of each IEnumerable value in the LookUp.


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 description for Employee
///
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" %>


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Demo on Lookup(TKey,TElement) Classtitle>
    <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 radiobutton 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 object. "

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

empList.ToLookup(c => c.MartialStatus) will  pass  LookupEmployee>  to employeeLookup Collection. The Lookup collection is created on key type of Boolean. Hence we can access the elements as follows,
The demo can be downloaded from
here.  DOWNLOAD.


 

Popular posts from this blog

Exporting to excel from a custom class object using C#.NET

How to hide bootStrap popover when user click(s) outside the popover?

Get the Clicked Cell Value and Column Name of GridView in ASP.NET